withLock(_:)
Calls the given closure after acquiring the lock and then releases ownership.
Declaration
borrowing func withLock<Result, E>(_ body: (inout sending Value) throws(E) -> sending Result) throws(E) -> sending Result where E : Error, Result : ~CopyableParameters
- body:
A closure with a parameter of
Valuethat has exclusive access to the value being stored within this mutex. This closure is considered the critical section as it will only be executed once the calling thread has acquired the lock.
Return Value
The return value, if any, of the body closure parameter.
Discussion
This method is equivalent to the following sequence of code:
mutex.lock()
defer {
mutex.unlock()
}
return try body(&value)