Skip to content

Instantly share code, notes, and snippets.

@jmcd
Created April 20, 2016 07:44
Show Gist options
  • Save jmcd/b54b963fcec4ec981eb493e0e216c721 to your computer and use it in GitHub Desktop.
Save jmcd/b54b963fcec4ec981eb493e0e216c721 to your computer and use it in GitHub Desktop.
import Foundation
class ObjectWithCriticalResource: NSObject {
private let semaphore = dispatch_semaphore_create(1)
func doTask() {
guard dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW) == 0 else {
// abort this call without doing the task
return
}
self.doTaskImpl()
}
private func doTaskImpl() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment