Skip to content

Instantly share code, notes, and snippets.

@tom-biel
Last active October 19, 2019 23:00
Show Gist options
  • Save tom-biel/30d78886ec904440d92cc591a42cfe5a to your computer and use it in GitHub Desktop.
Save tom-biel/30d78886ec904440d92cc591a42cfe5a to your computer and use it in GitHub Desktop.
Objectice-C @synchronized replacement for Swift
@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