# Async/await thread

**URL:** <https://forums.swift.org/t/async-await-thread/50046>\
**Category:** Using Swift\
**Tags:** dispatch, asynchronous, asyncawait\
**Created:** [June 27, 2021, 3:19am UTC](https://forums.swift.org/t/async-await-thread/50046 "2021-06-27T03:19:19Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [June 30, 2021, 9:10am UTC](https://forums.swift.org/t/async-await-thread/50046/9 "2021-06-30T09:10:53Z")

</div>

> i presume that for this question … the answer is "no"?

That’s not correct. The trick here is the `@MainActor` attribute, as discussed [here](https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md#closures). Consider this snippet:

```swift
@IBAction
private func testAction(_ sender: Any) {
    NSLog("testAction(_:)")
    async { @MainActor in
        NSLog("closure")
        await self.delay()
    }
}

private func delay() async {
    NSLog("delay()")
    await Task.sleep(1 * 1000 * 1000 * 1000)
}

```

This prints:

```swift
2021-06-30 09:50:38.913443+0100 xxsi13[54758:3760684] testAction(_:)
2021-06-30 09:50:38.914759+0100 xxsi13[54758:3760684] closure
2021-06-30 09:50:38.914915+0100 xxsi13[54758:3760684] delay()

```

Note how the thread ID (3760684) is the same in all cases.

**IMPORTANT** I tested this in Xcode 13.0b2 targeting the iOS 15.0 simulator. To get it to work I had to set the `SWIFT_DEBUG_CONCURRENCY_ENABLE_COOPERATIVE_QUEUES` environment variable, but I believe that’s because of an implementation bug rather than anything fundamentally wrong with my code.

Having said that, this stuff is moving _very_ fast so I could be wrong (-:

* * *

Oh, one last thing: The Swift concurrency design has an affordance for something called a _custom executor_, which will give you more control over how actors integrate with queues (or threads). However, this isn’t yet designed, let alone implemented. If you’re curious, see [Support custom executors in Swift concurrency](https://forums.swift.org/t/support-custom-executors-in-swift-concurrency/44425).

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

_[View the full topic](https://forums.swift.org/t/async-await-thread/50046)._
