Junior — Middle
Kas on Lock mõiste mitme- või sünkroniseerimise kontekstis?
sobes.tech AI
Vastus AI-lt
Lock (bloke) is a synchronization mechanism used to manage access to shared resources in a multithreaded environment. It ensures that only one thread can execute the critical section of code at a time, preventing race conditions and ensuring data integrity.
On iOS, objects like NSLock, DispatchSemaphore, or @synchronized are often used.
Example of using NSLock:
let lock = NSLock()
func criticalSection() {
lock.lock()
// code that should be executed by only one thread
lock.unlock()
}
Without locking, multiple threads could modify data simultaneously, leading to errors.