This file contains hidden or 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
| let thread = Thread { | |
| print(Thread.callStackSymbols) | |
| } | |
| thread.start() |
This file contains hidden or 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
| let thread = Thread { | |
| print(Thread.callStackSymbols) | |
| } | |
| thread.start() |
This file contains hidden or 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
| let task = Task { | |
| print("Start task") // prints | |
| try? await Task.sleep(for: .seconds(1)) | |
| print("Haven't checked cancellation yet") // prints | |
| try Task.checkCancellation() // or, if Task.isCancelled { return } | |
| print("End task") // does not print | |
| } | |
| task.cancel() |
This file contains hidden or 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
| let thread = Thread { | |
| print("NSThread starting...") | |
| Thread.exit() // exit thread internally | |
| print("Will never be printed") | |
| } | |
| thread.start() | |
| thread.cancel() // cancel thread externally |
This file contains hidden or 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
| enum User { | |
| @TaskLocal static var name = "n/a" | |
| } | |
| Task { | |
| print(User.name) // n/a | |
| User.$name.withValue("Alice") { | |
| print(User.name) // Alice | |
| Task { | |
| print(User.name) // Alice |
This file contains hidden or 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
| Thread { | |
| Thread.current.threadDictionary["name"] = "Alice" | |
| print(Thread.current.threadDictionary["name"]!) // Alice | |
| Thread.current.threadDictionary["name"] = "Bob" | |
| print(Thread.current.threadDictionary["name"]!) // Bob | |
| }.start() |
This file contains hidden or 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
| Task { @MainActor in | |
| print("Task scheduled on main actor") | |
| } |
This file contains hidden or 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
| Task.detached { | |
| print("Running on global actor") | |
| await MainActor.run { | |
| print("Switched to main actor") | |
| } | |
| } |
This file contains hidden or 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
| Thread { | |
| // <NSThread: 0x6000017cfd00>{number = 8, name = (null)} | |
| print("Running on background thread: \(Thread.current)") | |
| DispatchQueue.main.async { | |
| // <_NSMainThread: 0x600001708040>{number = 1, name = main} | |
| print("Switched to main thread: \(Thread.current)") | |
| } | |
| }.start() |
This file contains hidden or 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
| Task(priority: .high) { | |
| print(Task.currentPriority) // TaskPriority.high | |
| } |
NewerOlder