UnfairLock

Swift

final public class UnfairLock

https://www.mikeash.com/pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html os_unfair_lock can be a good choice when per-lock overhead is important (because for some reason you have a huge number of them) and you don’t need fancy features. It’s implemented as a single 32-bit integer which you can place wherever you need it, so overhead is small. As the name hints, one of the features that os_unfair_lock is missing is fairness. Lock fairness means that there’s at least some attempt to ensure that different threads waiting on a lock all get a chance to acquire it. Without fairness, it’s possible for a thread that rapidly releases and re-acquires the lock to monopolize it while other threads are waiting.

  • Undocumented

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func lock()
  • Undocumented

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func tryLock() -> Bool
  • Undocumented

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func unlock()
  • Undocumented

    Declaration

    Swift

    @discardableResult
    @inlinable
    @inline(__always)
    public func withCriticalScope<Result>(body: () throws -> Result) rethrows -> Result