# Main dispatch queue in Linux/SDL app?

**URL:** <https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708>\
**Category:** Using Swift\
**Tags:** linux\
**Created:** [December 16, 2019, 3:54am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708 "2019-12-16T03:54:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [December 16, 2019, 3:54am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/1 "2019-12-16T03:54:24Z")

</div>

I've got a pretty decent little macOS/Linux cross-platform app working with SDL2 now, but I just ran into an issue where using `DispatchQueue.main.async {}` doesn't work on Linux; the block never executes. It does, however, execute on macOS.

The main part of my program is a loop that waits on the SDL event queue at the start of each loop. It then handles the events (which are mostly timers firing) that usually initiate network requests.

My guess is SDL’s macOS implementation of `SDL_WaitEvent()` is allowing the main queue to run other blocks, but not the Linux implementation. Is there a call I can make to let my main loop yield to GCD so other blocks can run? Any suggestion on how to go idle until an SDL event is available?

Thanks.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [December 16, 2019, 5:42am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/2 "2019-12-16T05:42:56Z")

</div>

I think you need to call `dispatchMain()` to get the main queue running. I’m guessing the Mac version has something calling it implicitly.

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [December 16, 2019, 9:15am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/3 "2019-12-16T09:15:15Z")

</div>

> Any suggestion on how to go idle until an SDL event is available?

On Apple platforms, the main thread’s run loop services the main dispatch queue. Search [swift-corelibs-foundation](https://github.com/apple/swift-corelibs-foundation) for `_dispatch_main_queue_callback_4CF` to see how this works. Looking at the code, it seems to be set up to function on Linux, leaving you with one of two possibilities:

- There’s some sort of bug.

- SDL’s main loop is not running the run loop on Linux

It’s seems likely it’s the latter.

* * *

As to how you fix this, there’s a bunch of potential approaches you can take, but the most important thing is how you integrate with SDL’s abstractions. I think you should dig into `SDL_WaitEvent` to see how it works on Apple platforms. Given that Swift’s Foundation brings run loop along with it, it’s possible you could just switch it over to using run loops on Linux.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [December 17, 2019, 1:37am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/4 "2019-12-17T01:37:17Z")

</div>

I haven’t been able to figure out what SDL does on macOS if you don’t instantiate `SDLApplication` (a subclass of `NSApplication`), which I don’t. Grepping through the code I haven’t been able to find calls to `dispatchMain()` or an obvious `NSRunLoop` setup (there are run loops in some HID and audio code, and calls to get the global dispatch queue, but nothing obvious).

The event handling for Cocoa (which I’m not at all sure is what’s at play in a macOS build) calls `[NSApp nextEventMatchingMask:…]`, and `[NSApp sendEvent:]`, but since I'm not instantiating `NSApplication`, I don't think that’s involved. Not at all sure, though.

I’ve thought of a way to just call `dispatchMain()` in Linux-only portions of my code, looping on `SDL_WaitEvent()` on a separate queue and dispatching those back to the main queue. I haven’t tried it yet, but I think it’ll work.

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [December 17, 2019, 8:33am UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/5 "2019-12-17T08:33:20Z")

</div>

> I haven’t been able to figure out what SDL does on macOS if you don’t  
> instantiate `SDLApplication` …. Grepping through the code I haven’t  
> been able to find calls to `dispatchMain` or an obvious `NSRunLoop`  
> setup

One trick for uncovering this mechanism is to set a breakpoint on one of your event handlers and then look at the backtrace.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [December 17, 2019, 9:33pm UTC](https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/6 "2019-12-17T21:33:44Z")

</div>

Alas, one typically calls into SDL to request the next event. However, there is an event watcher mechanism that might shed light on the situation. Thanks!
