Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active May 27, 2022 04:13
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 robertmryan/c5b81597b87798342b011457412cb71f to your computer and use it in GitHub Desktop.
Save robertmryan/c5b81597b87798342b011457412cb71f to your computer and use it in GitHub Desktop.
func foo(threads: Int) {
print("starting \(threads) threads")
let semaphore = DispatchSemaphore(value: 0)
let group = DispatchGroup()
for i in 0 ..< threads {
DispatchQueue.global().async(group: group) {
semaphore.wait()
print("done waiting \(i)")
}
}
group.notify(queue: .main) {
print("all \(threads) threads are done")
}
DispatchQueue.global().async {
print("signaling")
for _ in 0 ..< threads {
semaphore.signal()
}
}
}
foo(threads: 63) // this won't deadlock
foo(threads: 64) // this will deadlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment