swiftlang / swift-corelibs-libdispatch

The libdispatch Project, (a.k.a. Grand Central Dispatch), for concurrency on multicore hardware
swift.org
Apache License 2.0
2.47k stars 460 forks source link

`DispatchQueue.main` is not bound to main thread on Windows #846

Open HeMet opened 1 month ago

HeMet commented 1 month ago
@main
struct EntryPoint3: AsyncParsableCommand {
    @MainActor
    mutating func run() async throws {
        print("Thread is main: \(Thread.isMainThread)")
        print("Current Thread ID: \(GetCurrentThreadId())")
        MainActor.preconditionIsolated()
        await withCheckedContinuation { con in
            DispatchQueue.main.async {
                print("Thread is main (queue): \(Thread.isMainThread)")
                print("Current Thread ID (queue): \(GetCurrentThreadId())")
                MainActor.preconditionIsolated()
                con.resume()
            }
        }
        print("completed")
    }
}

Output:

Thread is main: false
Current Thread ID: 498188
Thread is main (queue): false
Current Thread ID (queue): 498188
completed

Thread on Swift Forums https://forums.swift.org/t/mainactor-behaves-different-on-windows-and-macos/74742