Remix.run Logo
physicsguy 5 hours ago

> Recommendation: replace your program’s threading with Dispatch.

The Apple assumption that people writing software they want to work on MacOS isn't cross platform strikes again.

lunacookies 2 hours ago | parent | next [-]

Author here! I’d suggest that you treat Dispatch the same way you’d treat, say, pthread_create¹ on Unix systems or CreateThread² on Windows – each platform has some features unique to it, but that doesn’t mean it’s impossible to make a cross-platform wrapper, nor does it mean that you need to restrict yourself to a lowest-common-denominator subset. In many cases it’s possible to create a cross-platform wrapper which uses platform-specific functionality where it can to suit your use-case.

Wrapping the core functionality of Dispatch I’m suggesting you use (workloops and serial queues) in a cross-platform manner isn’t difficult. On non-Apple platforms you do the standard thing: create a thread using those functions I mentioned above, have the thread sit in a loop receiving from a channel, and give the thread work to do by sending messages to the channel. On Apple platforms you’d instead create the “thread” with dispatch_workloop_create³, and you’d replace the places where you send to the channel with a call to dispatch_async⁴. Of course, this doesn’t give you any of the QoS behavior of Dispatch on non-Apple platforms, but that’s fine.

If you want to see an admittedly-complex example, WebKit’s WorkQueue⁵ is a cross-platform Dispatch wrapper.

1: https://pubs.opengroup.org/onlinepubs/9699919799.orig/functi... 2: https://learn.microsoft.com/en-us/windows/win32/api/processt... 3: https://developer.apple.com/documentation/dispatch/3191906-d... 4: https://developer.apple.com/documentation/dispatch/1452834-d... 5: https://github.com/WebKit/WebKit/blob/88039a970adb50d9cf382e...

krackers 5 hours ago | parent | prev | next [-]

libdispatch is open-source and cross platform though.

physicsguy 5 hours ago | parent | next [-]

Heh, news to me. I've always stuck with OpenMP for cross platform across Mac/Windows/Linux

jshier 4 hours ago | parent [-]

Apple specifically compiles OpenMP support out of their builds of clang, so that's never been an easy solution on macOS. Even worse if you want to run on other Apple platforms where an external compiler can't be used.

physicsguy 4 hours ago | parent | next [-]

In practice the software I’ve worked on has typically been engineering/scientific. Back in older pre-ARM days that meant Intel compiler toolchain on all platforms.

cvadict 3 hours ago | parent | prev [-]

Iirc. you just need the openMP libraries from a compatible version of clang.

5 hours ago | parent | prev [-]
[deleted]
wat10000 5 hours ago | parent | prev [-]

Pthreads are available and work fine if you want cross-platform code.