<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Something has to drain blocks sent to the main queue.<div><br></div><div>You can run a Foundation run loop. That should drain the main queue. I believe it also ensures blocks sent to the main queue always run on the main thread.</div><div><br></div><div>Dispatch includes its own lower-level function to pump the main queue: dispatch_main. This does not return. It does not ensure main queue blocks run on the main thread. The main queue does remain the only default, global, serial queue.<br><br>Both the Foundation and Dispatch APIs support creating custom event sources. A custom event source lets your integrate processing your own events with the framework’s main run loop. You might investigate those event source APIs as a way to integrate your SDL rendering code with whichever run loop you select. (If you expect to use Foundation, you probably want to run its RunLoop.)</div><div><br></div><div>Don’t forget to drain the autorelease pool associated with your main run loop regularly.</div><div><br></div><div>Cheers,<br><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">--</span><div><span style="background-color: rgba(255, 255, 255, 0);">Jeremy W. Sherman</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><a href="http://jeremywsherman.com/">http://jeremywsherman.com/</a></span></div></div><div><br>On Oct 4, 2017, at 21:07, Rahul Ranjan via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div>Hi Geordie,<br><br>I am new here and so pardon me if I misunderstood anything.<br><br>When you run a program in Swift, it already runs on main thread by default and then you can all Dispatch async. So for example,<br><br>while true { // This while loop will run for infinite on main thread<br>  <br>    Dispatch.main.async {} // your rendering goes here.<br><br>}<br><br>Regards,<br>Rahul<br><div class="gmail_quote"><div dir="ltr">On Thu, 5 Oct 2017 at 2:45 AM, Geordie J via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<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">Hi!<div><br></div><div>I have a somewhat unusual use-case that I’m trying to figure out.</div><div><br></div><div>I have a command-line swift app whose main thread ends up in an infinite loop for graphics rendering purposes (running SDL).</div><div><br></div><div>What I’m hoping to do is to be able to run async code like this from within that infinite loop:</div><div><br></div><div><i>DispatchQueue.someQueue.async {</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">        </span>// do something</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">        </span>DispatchQueue.main.async {</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">                </span>// return result</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">        </span>}</i></div><div><i>}</i></div><div><br></div><div>The issue is that the <i>DispatchQueue.main.async</i> block never runs, which is pretty logical given that it’s called&nbsp;<i>inside&nbsp;</i>the infinite loop - so where would it possibly get to run?</div><div><br></div><div><br></div><div>The question is, how do we achieve this? Is there a better way to have an infinite loop on the main thread (e.g. Foundation’s RunLoop class etc)? On iOS (where I have the most experience with Swift), the rendering loop’s implementation is obviously hidden.</div><div><br></div><div>Theoretically we could probably do the entire rendering loop like this:</div><div><br></div><div><i>func renderStuff() {</i></div><div><i><br></i></div><div><span class="m_8520450962015912979Apple-tab-span" style="font-style:italic;white-space:pre-wrap">        </span><i>//&nbsp;actually&nbsp;render stuff</i></div><div><i><br></i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">        </span>if !shouldQuit {</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">                </span>DispatchQueue.main.async(renderStuff) // loops here</i></div><div><i><span class="m_8520450962015912979Apple-tab-span" style="white-space:pre-wrap">        </span>}</i></div><div><i>}</i></div><div><i><br></i></div><div><i>// Start render loop:</i></div><div><i>DispatchQueue.main.async(renderStuff)</i></div><div><br></div><div>But I am pretty sure the DispatchQueue API is not intended to be used like that :)</div><div><br></div><div>Any ideas? Maybe an API that basically says “dequeue and perform work from the DispatchQueue”?</div><div><br></div><div>Cheers,</div><div>Geordie</div><div><br></div></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div><div dir="ltr">-- <br></div><div class="gmail_signature" data-smartmail="gmail_signature">Rahul<br><br>Sent from iPhone<br><br></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></div></body></html>