Last active
October 19, 2019 23:00
-
-
Save tom-biel/30d78886ec904440d92cc591a42cfe5a to your computer and use it in GitHub Desktop.
Objectice-C @synchronized replacement for Swift
This file contains 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
@inline(__always) | |
func synchronized<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T { | |
objc_sync_enter(lock) | |
defer { objc_sync_exit(lock) } | |
return try body() | |
} | |
// example of usage | |
synchronized (self) {[weak self] in | |
// code here | |
// code here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment