Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS equivalent of the Windows Fibers API?

I'm asking this out of curiosity.

Windows provides what they call a Fibers API, which is a API for lightweight user processes/threads.

I was interested in knowing if Mac OS provides such features as well. As far as I could find out, the closest Unix equivalent to that would be the setcontext family of functions. However, trying to call such API on a Mac program produces warnings saying that the functions have been deprecated since OSX 10.6. Also, when I attempt to compile and run the example provided in the Wikipedia link above, I get a seg fault on my machine at the first swapcontext.

So apparently the setcontext API is a no go for Mac. Not any longer at least. Is there any other way to achieve lightweight user-side threads on Mac OS? Does the system provide such functionality?

like image 414
glampert Avatar asked May 18 '15 02:05

glampert


1 Answers

No, there is no equivalent on OS X (or most UNIX-based systems, for that matter). The ucontext series of functions were deprecated by the POSIX standard and no replacement was provided.

The closest you can get on OS X is Grand Central Dispatch, which allows you to create dispatch queues that execute 'blocks' (essentially functions). Processing of these queues can be suspended and resumed, similar to fibers, though you can't stop and resume execution in the middle of a block.

There's also Boost.Context, which provides similar functionality to ucontext (and perhaps even uses it internally), though it's a C++ library.

like image 153
Collin Dauphinee Avatar answered Oct 26 '22 23:10

Collin Dauphinee