<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=""><br class=""><div class="">

</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On Dec 22, 2017, at 10:59 AM, Pierre Habouzit via swift-corelibs-dev &lt;<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none;" class=""><blockquote type="cite" class=""><div class="">On Dec 21, 2017, at 10:57 AM, Geordie J via swift-corelibs-dev &lt;<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Hi,<div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class=""><i class="">print(Thread.isMainThread) // true</i></div><div class=""><i class="">DispatchQueue.main.<b class="">async</b><span class="Apple-converted-space">&nbsp;</span>{</i></div><div class=""><i class="">&nbsp; &nbsp; &nbsp;print(Thread.isMainThread) // false (???)</i></div><div class=""><i class="">}</i></div></div></div></blockquote><div class=""><br class=""></div><div class="">This is wrong.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""><br class=""></div><div class="">Whereas this same code but with “sync" prints “true” “true”:</div><div class=""><br class=""></div><div class=""><div class=""><i class="">print(Thread.isMainThread) // true</i></div><div class=""><i class="">DispatchQueue.main.<b class="">sync</b><span class="Apple-converted-space">&nbsp;</span>{</i></div><div class=""><i class="">&nbsp; &nbsp; &nbsp;print(Thread.isMainThread) // true</i></div><div class=""><i class="">}</i></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">This is expected, because dispatch_sync() on the main thread is... difficult.</div></div></div></blockquote><div><br class=""></div><div>actually it should deadlock, this is wrong ;)</div><div>dispatch_sync() on the main queue like I said has weird semantics, and doing dispatch_sync() from the main thread onto the main queue is illegal.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none;" class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""><br class=""></div><div class="">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?</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Are there any resources or docs to get up to scratch with the internals of Dispatch a bit better?</div></div></div></blockquote><div class=""><br class=""></div><div class="">THe problem here is not Dispatch, it's how it's (mis-)ported to Android for now.</div><div class=""><br class=""></div><div class="">On other platforms, either you call dispatch_main() and there's no main thread anymore, or from the main thread you are supposed to drain the Main CFRunloop which in turn is integrated with dispatch (or rather the other way around: dispatch is integrated with the main CFRunloop) and the main queue acts as a source on the main CFRunloop.</div><div class=""><br class=""></div><div class="">Last I heard, Android has not done that. I tried to explain this on&nbsp;<a href="https://github.com/apple/swift-corelibs-libdispatch/pull/299" class="">https://github.com/apple/swift-corelibs-libdispatch/pull/299</a>&nbsp;which atm is still not resolved.</div><div class=""><br class=""></div><div class="">-Pierre</div></div><span style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none; float: none; display: inline !important;" class="">_______________________________________________</span><br style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none; float: none; display: inline !important;" class="">swift-corelibs-dev mailing list</span><br style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none;" class=""><a href="mailto:swift-corelibs-dev@swift.org" style="font-family: SFMono-Light; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-corelibs-dev@swift.org</a><br style="caret-color: rgb(0, 0, 0); font-family: SFMono-Light; font-size: 11px; 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; text-decoration: none;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-corelibs-dev" style="font-family: SFMono-Light; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</a></div></blockquote></div><br class=""></body></html>