Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Created December 16, 2022 06:21
Show Gist options
  • Save boraseoksoon/613e6067a3881d52cd7f7a875c263679 to your computer and use it in GitHub Desktop.
Save boraseoksoon/613e6067a3881d52cd7f7a875c263679 to your computer and use it in GitHub Desktop.
cooperative thread pool test code
import Foundation
// reference: https://stackoverflow.com/questions/67978028/maximum-number-of-threads-with-async-await-task-groups
func run() async {
await withTaskGroup(of: Void.self) { group in
for i in 0 ..< 32 {
group.addTask {
print(await fire(i))
}
}
}
}
func run2() {
DispatchQueue.global().async {
DispatchQueue.concurrentPerform(iterations: 32) { i in
print(fire2(i))
}
}
}
func fire(_ i: Int) async -> Int { i }
func fire2(_ i: Int) -> Int { i }
Task {
await run()
}
run2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment