Created
November 15, 2016 16:24
-
-
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.
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
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