CriticalSection
public struct CriticalSection
CriticalSections can be used to ensure exclusive access to a given resource.
By protecting a resource with a CriticalSection, you ensure that only one
one thread can be executing code using that critical section at any given time.
CriticalSections are re-entrant, meaning that code already holding access
to the critical section may re-enter the critical section without causing
a deadlock. (The NSRecursiveLock class is used under the hood.)
-
Initializer.
Declaration
Swift
public init() -
Attempts to acquire exclusive access to the critical section before executing the passed-in function. The calling thread will block indefinitely until it is able to acquire the critical section.
Declaration
Swift
public func execute<T>(_ fn: () -> T) -> TParameters
fnThe function to execute once exclusive access to the critical section has been acquired.
-
Attempts to acquire exclusive access to the critical section before executing the passed-in function. The calling thread will block for at most
timeoutseconds while waiting to enter the critical section before giving up.Declaration
Swift
public func execute(timeout: TimeInterval, _ fn: () -> Void) -> BoolParameters
timeoutThe maximum time to wait while attempting to acquire exclusive access to the critical section.
fnThe function to execute once exclusive access to the critical section has been acquired.
Return Value
trueif exclusive access to the critical section was acquired andfnwas executed.falseiftimeoutexpired andfnwas not executed.
View on GitHub
CriticalSection Struct Reference