Lock

public class Lock

A mutex-type thread lock. The lock can be held by only one thread. Other threads attempting to secure the lock while it is held will block. The lock is initialized as being recursive. The locking thread may lock multiple times, but each lock should be accompanied by an unlock.

  • Initialize a new lock object.

    Declaration

    Swift

    public init()
  • Attempt to grab the lock. Returns true if the lock was successful.

    Declaration

    Swift

    public func lock() -> Bool
  • Attempt to grab the lock. Will only return true if the lock was not being held by any other thread. Returns false if the lock is currently being held by another thread.

    Declaration

    Swift

    public func tryLock() -> Bool
  • Unlock. Returns true if the lock was held by the current thread and was successfully unlocked. ior the lock count was decremented.

    Declaration

    Swift

    public func unlock() -> Bool
  • Acquire the lock, execute the closure, release the lock.

    Declaration

    Swift

    public func doWithLock(closure: () throws -> ()) rethrows