[swift-corelibs-dev] DispatchQueue and Threads

Geordie J geojay at gmail.com
Thu Dec 21 12:56:57 CST 2017


Hi,

We are having some issues with threading / dispatch queues this week. Basically I’m wondering why the following “async” code has the output it does:

print(Thread.isMainThread) // true
DispatchQueue.main.async {
     print(Thread.isMainThread) // false (???)
}

Whereas this same code but with “sync" prints “true” “true”:

print(Thread.isMainThread) // true
DispatchQueue.main.sync {
     print(Thread.isMainThread) // true
}

This goes against what I thought I understood about Dispatch. Are there any threading guarantees made by DispatchQueue at all? Especially on the main queue. And if not, is there another construct we can use?

The issue is that we’re on Android (where the threading situation is cloudy at best to begin with) and the JNI (Java-Native Interface) has specific requirements about which threads its methods are run on. Let alone our graphics rendering code.

Basically, initialising and using the JNI in separate DispatchQueue.main.async blocks apparently does not end up on the same thread (and apparently neither of them are the main thread). I’m quite confused by this.

Are there any resources or docs to get up to scratch with the internals of Dispatch a bit better?


Thanks for your ears and eyes :)
– Geordie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20171221/4c54decf/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: Message signed with OpenPGP
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20171221/4c54decf/attachment.sig>


More information about the swift-corelibs-dev mailing list