Created
March 7, 2021 17:13
-
-
Save robertmryan/fb65510f42e315f24859cd5a24efae4f 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
// The following results in thread explosion: | |
for idx in 0..<10_000 { | |
concurrentQueue.async(group: group) { | |
self.something.n += idx | |
} | |
} | |
// We would instead do | |
concurrentQueue.async { | |
DispatchQueue.concurrentPerform(iterations: 10_000) { idx in | |
self.something.n += idx | |
} | |
} |
Needless to say, this isn’t thread-safe operation. I’m just showing how to avoid thread issue explosion.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By using
concurrentPerform
, the number of GCD worker threads is limited to the number of cores on the device.