6

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?

glampert
  • 4,371
  • 2
  • 23
  • 49
  • 1
    Have you looked at Grand Central Dispatch? – Scott Hunter May 18 '15 at 02:56
  • @ScottHunter, I know some of the GCD framework. But does it allow, for instance, manually switching between threads of work, like it is with the fibers? I don't think GCD could be considered as an equivalent to user-side threads... – glampert May 18 '15 at 03:48
  • I wouldn't necessarily give up on the Mac OS setcontext() quite so quickly... deprecated functions usually still work; they just aren't guaranteed to be supported in future OS releases. So it might be just a matter of tweaking some detail to get setcontext() working under MacOS. – Jeremy Friesner May 18 '15 at 03:53
  • There are OS X C APIs are deprecated with no replacement, unless you use Obj-C. You can usually ignore their deprecated warnings. – Collin Dauphinee May 21 '15 at 01:03
  • @CollinDauphinee, yes, thanks for the input. Since I asking this to inform myself mainly, I'm not bound to any specific language. Do you happen to know if a Fibers-like API is exposed on MacOS in Objective-C? – glampert May 21 '15 at 02:33

1 Answers1

3

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.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71