Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created December 30, 2022 10:44
Show Gist options
  • Save alexito4/549a9c1e634269e1100cf57754e069ed to your computer and use it in GitHub Desktop.
Save alexito4/549a9c1e634269e1100cf57754e069ed to your computer and use it in GitHub Desktop.
print("Let's span a bunch of tasks and see how many cooperative threads are created.")
await withTaskGroup(of: Void.self) { group in
for i in 1...100 {
group.addTask {
print("Enter \(i)")
for n in 1...100000 {
let a = i * n
print(a)
}
try? await Task.sleep(for: .seconds(2))
for n in 1...100000 {
let a = i * n
print(a)
}
print("Exit \(i)")
}
}
}
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment