Locking
-
Declaration
Swift
public protocol LockingProtocol : AnyObject
-
A mutually exclusive (or mutex) lock.
A mutex is a type of semaphore that grants access to only one thread at a time. If a mutex is in use and another thread tries to acquire it, that thread blocks until the mutex is released by its original holder. If multiple threads compete for the same mutex, only one at a time is allowed access to it.
You instantiate a re-entrant variant by passing
true
toinit(recursive:)
. A recursive lock may be acquired multiple times by the same thread without causing a deadlock, a situation where a thread is permanently blocked waiting for itself to relinquish a lock. While the locking thread has one or more locks, all other threads are prevented from accessing the code protected by the lock.Both types of locks check for usage errors. See
PTHREAD_MUTEX_ERRORCHECK
andPTHREAD_MUTEX_RECURSIVE
inman 3 pthread_mutexattr_settype
.Backed by
See morepthread_mutex
.Declaration
Swift
public final class PosixLock : LockingProtocol
-
-
Low-level lock that allows waiters to block efficiently on contention.
There is no attempt at fairness or lock ordering, e.g. an unlocker can potentially immediately reacquire the lock before a woken up waiter gets an opportunity to attempt to acquire the lock. This may be advantageous for performance reasons, but also makes starvation of waiters a possibility.
Backed by
See moreos_unfair_lock
.Declaration
Swift
public final class UnfairLock : LockingProtocol
-
Declaration
Swift
public final class SpinLock : LockingProtocol
-
Declaration
Swift
public final class SharedValue<T, LockType> where LockType : LockingProtocol