Locking

  • 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 to init(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 and PTHREAD_MUTEX_RECURSIVE in man 3 pthread_mutexattr_settype.

    Backed by pthread_mutex.

    See more

    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 os_unfair_lock.

    See more

    Declaration

    Swift

    public final class UnfairLock : LockingProtocol