<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Those GCD examples make sense to me on the surface actually...</div><div class=""><br class=""></div><div class="">print(Thread.isMainThread) // true (actually on the main thread)<br class="">DispatchQueue.main.async {<br class=""> print(Thread.isMainThread) // false (on some libdispatch/system owned thread)<br class="">}</div><div class=""><br class=""></div><div class="">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).</div><div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class="">print(Thread.isMainThread) // true<br class="">DispatchQueue.main.sync {<br class=""> print(Thread.isMainThread) // true<br class="">}</div><div class=""><br class=""></div><div class="">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 <a href="https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync" class="">https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync</a>).</div><div class=""><br class=""></div><div class="">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).</div><div class=""><br class=""></div><div class="">SL</div><div class=""><br class=""></div><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 22, 2017, at 12:57 PM, <a href="mailto:swift-corelibs-dev-request@swift.org" class="">swift-corelibs-dev-request@swift.org</a> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">DispatchQueue and Threads</span></div></blockquote></div><br class=""></body></html>