Created
November 20, 2023 09:22
-
-
Save cntrump/f04714f64e56d7100d52ed20ab8cd4bd to your computer and use it in GitHub Desktop.
@synchronized in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ObjectiveC | |
@discardableResult | |
public func synchronized<T>(_ lock: AnyObject, closure: () -> T) -> T { | |
let lock_ = lock | |
objc_sync_enter(lock_) | |
defer { objc_sync_exit(lock_) } | |
return closure() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example code: