Last active
May 27, 2022 04:13
-
-
Save robertmryan/c5b81597b87798342b011457412cb71f to your computer and use it in GitHub Desktop.
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
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