import Foundation
let task = Task {
print("isMainThread = \(Thread.isMainThread), isMainActor = \(#isolation === MainActor.shared)")
}
_ = await task.result
Prints isMainThread = false, isMainActor = true for me (Swift 6.0.2, Windows and Linux, not tested on a Mac).
Shouldn't we have isMainActor = false here?
Surely main thread does not guarantee that we are on MainActor. Other actors can also run on main thread.
But conversely, MainActor should guarantee that we are on main thread, because MainActor should guarantee is it safe to modify UI, which is only safe on main thread.
This code snippet obviously outputs wrong result. Or I'm missing something?
The result is different on macOS. On macOS 15.1.1, this prints isMainThread = true, isMainActor = true (compiled in Swift 5 mode because accessing isMainThread is an error in Swift 6 mode):
$ swift --version
swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
Target: arm64-apple-macosx15.0
$ swiftc IsMainThread.swift
… warning: class property 'isMainThread' is unavailable from asynchronous contexts …
$ ./IsMainThread
isMainThread = true, isMainActor = true
Hmm... Not only on both Windows and Linux the output is different, there is also no warnings about isMainThread in Swift 6 mode on both these platforms.
I cannot find thread right away, but there was a discussion some time ago that notion of main thread apart from Apple platforms isn’t well defined and main actor isn’t guarantee to stick with thread check there. You can try searching forum, there was a more detailed explanation than I gave here.