<div><div dir="auto">Hi Pierre</div><div dir="auto"><br></div><br><div class="gmail_quote"><div dir="auto">Pierre Habouzit &lt;<a href="mailto:pierre@habouzit.net">pierre@habouzit.net</a>&gt; schrieb am Fr. 22. Dez. 2017 um 19:59:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div>On Dec 21, 2017, at 10:57 AM, Geordie J via swift-corelibs-dev &lt;<a href="mailto:swift-corelibs-dev@swift.org" target="_blank">swift-corelibs-dev@swift.org</a>&gt; wrote:</div><br class="m_3957295752623447889Apple-interchange-newline"><div><div style="word-wrap:break-word;line-break:after-white-space">Hi,<div><br></div><div>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><br></div><div><i>print(Thread.isMainThread) // true</i></div><div><i>DispatchQueue.main.<b>async</b> {</i></div><div><i>     print(Thread.isMainThread) // false (???)</i></div><div><i>}</i></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div>This is wrong.</div></div></div></blockquote><div dir="auto"><br></div><div dir="auto">This is wrong as in, it should be the main thread? That’s what I thought.</div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><br><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div><div>Whereas this same code but with “sync&quot; prints “true” “true”:</div><div><br></div><div><div><i>print(Thread.isMainThread) // true</i></div><div><i>DispatchQueue.main.<b>sync</b> {</i></div><div><i>     print(Thread.isMainThread) // true</i></div><div><i>}</i></div></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div>This is expected, because dispatch_sync() on the main thread is... difficult.</div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><br><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div><div>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><br></div><div>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><br></div><div>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><br></div><div>Are there any resources or docs to get up to scratch with the internals of Dispatch a bit better?</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div>THe problem here is not Dispatch, it&#39;s how it&#39;s (mis-)ported to Android for now.</div><div><br></div><div>On other platforms, either you call dispatch_main() and there&#39;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><br></div><div>Last I heard, Android has not done that. I tried to explain this on <a href="https://github.com/apple/swift-corelibs-libdispatch/pull/299" target="_blank">https://github.com/apple/swift-corelibs-libdispatch/pull/299</a> which atm is still not resolved.</div></div></div></blockquote><div dir="auto"><br></div><div dir="auto">Ok this is the info I needed, thanks! Apparently the toolchain I’m running has had this patch applied to it. That would explain why .sync doesn’t deadlock, why .async isn’t on the main thread, and why running .main.async repeatedly ends up running the blocks on different threads.</div><div dir="auto"><br></div><div dir="auto">I will rebuild dispatch and try again. </div><div dir="auto"><br></div><div dir="auto">Thanks,</div><div dir="auto">Geordie</div><div dir="auto"><br></div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><br></div><div>-Pierre</div></div></div></blockquote></div></div>