Skip to content

Instantly share code, notes, and snippets.

@sidepelican
Created February 22, 2023 02:18
Show Gist options
  • Save sidepelican/8610be9627a5073af959711918d1ef5c to your computer and use it in GitHub Desktop.
Save sidepelican/8610be9627a5073af959711918d1ef5c to your computer and use it in GitHub Desktop.
Task.detachedはTask.initとは別のスレッドプールを持つが、無限に伸びるわけではない
var greeting = "Hello, playground"
print(greeting, ProcessInfo.processInfo.processorCount)
defer {
print(greeting)
}
for i in 0..<30 {
Task.detached {
print("detached", i)
sleep(1)
}
Task {
print(i)
sleep(1)
}
}
RunLoop.main.run(until: Date().addingTimeInterval(15))
Hello, playground 4
detached 0
detached 1
detached 2
0
1
detached 3
2
3
detached 4
detached 5
4
5
detached 6
detached 7
6
7
detached 9
detached 8
8
9
detached 10
detached 11
10
11
detached 12
detached 13
12
13
detached 15
detached 14
14
15
16
detached 16
detached 17
17
detached 18
detached 19
18
19
20
detached 20
detached 21
detached 22
22
23
detached 23
21
24
detached 24
detached 25
detached 26
25
26
detached 27
27
28
detached 28
29
detached 29
Hello, playground
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment