Those GCD examples make sense to me on the surface actually...
print(Thread.isMainThread) // true (actually on the main thread)
DispatchQueue.main.async {
print(Thread.isMainThread) // false (on some libdispatch/system owned
thread)
}
The following makes sense as the main DispatchQueue is not necessarily being
executed on the main thread — it is likely being executed on a pre-allocated
thread or thread pool managed by libdispatch (or the thread dispatchMain is
called on I believe).
However, when you move into sync, what you are actually doing is momentarily
blocking the existing thread and preventing the queue from executing anymore
until your block is over. So in:
print(Thread.isMainThread) // true
DispatchQueue.main.sync {
print(Thread.isMainThread) // true
}
Since you are on the main thread when you call sync, dispatch attempts to block
the thread until execution of the block is finished. Dispatch will attempt to
optimize sync calls by running them on the thread they are called from when
possible (since there is no point moving to a different thread and tying up the
calling thread to do nothing but wait) (see bottom of
https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync
<https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync>).
I believe both of these come down to where dispatchMain() is called (but
somebody with deeper expertise on libdispatch feel free to jump in an correct
anything erroneous).
SL
> On Dec 22, 2017, at 12:57 PM, [email protected] wrote:
>
> DispatchQueue and Threads
_______________________________________________
swift-corelibs-dev mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev