Skip to content

Instantly share code, notes, and snippets.

@makhocheung
Last active December 11, 2021 04:13
Show Gist options
  • Save makhocheung/26e3080407402a464ce6b8f47d6acb29 to your computer and use it in GitHub Desktop.
Save makhocheung/26e3080407402a464ce6b8f47d6acb29 to your computer and use it in GitHub Desktop.
A test of task
func wait() async {
await Task.sleep(1000000000)
}
Thread.current.name = "Main thread"
print("Thread in top-level is \(Thread.current.name)")
Task {
print("Thread in task before wait is \(Thread.current.name)")
if Thread.current.name!.isEmpty {
Thread.current.name = "Task thread"
print("Change thread name \(Thread.current.name)")
}
await wait()
print("Thread in task after wait is \(Thread.current.name)")
}
Thread.sleep(until: .now + 2)
// print as follow
// Thread in top-level is Optional("Main thread")
// Thread in task before wait is Optional("")
// Change thread name Optional("Task thread")
// Thread in task after wait is Optional("")
// Thread in task is different before and after `wait()`
// Thread in task is not "Main thread"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment