Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created November 15, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/dcff25a02bf0a744473914ce18fc86b7 to your computer and use it in GitHub Desktop.
Save codelynx/dcff25a02bf0a744473914ce18fc86b7 to your computer and use it in GitHub Desktop.
[swift3] using GCD's semaphore to protect critical resources accessing from multiple threads.
class MyObject {
let semaphore = DispatchSemaphore(value: 1)
func update1() {
semaphore.wait()
defer { semaphore.signal() }
// update some critical resources
}
func update2() {
semaphore.wait()
defer { semaphore.signal() }
// update some resources
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment