66

I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my Ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test.

sem_t sem1;
sem_t sem2;
sem_t sem3;
sem_t sem4;
sem_t sem5;
sem_t sem6;

sem_init(&sem1, 1, 1);
sem_init(&sem2, 1, 2);
sem_init(&sem3, 1, 3);
sem_init(&sem4, 1, 4);
sem_init(&sem5, 1, 5);
sem_init(&sem6, 1, 6);

The values appear to be random numbers, and they do not change after the sem_init call.

4
  • 4
    You should test the return value of sem_init.
    – bfontaine
    May 18, 2014 at 12:27
  • 1
    Note that in Mac OS X Yosemite (10.10) at least, sem_init(), sem_destroy() and sem_getvalue() are marked 'deprecated' and generate compiler warnings as a result. Jan 2, 2015 at 4:58
  • @JonathanLeffler Oh, no. It cannot work in Mac OS X Yosemite(10.10.1).
    – vipygd
    Jan 4, 2015 at 15:43
  • See also stackoverflow.com/questions/1413785/sem-init-on-os-x and stackoverflow.com/questions/641126/… Your options are to reimplement the needed api using GCD semaphore, mach semaphore, mutex + pthread condition vars, or mutex + pipes (to block and signal as needed)
    – 1110101001
    Oct 22 at 0:21

3 Answers 3

102

Unnamed semaphores are not supported, you need to use named semaphores.

To use named semaphores instead of unnamed semaphores, use sem_open instead of sem_init, and use sem_close and sem_unlink instead of sem_destroy.

3
  • 51
    Wow. <semaphore.h> declares sem_init so that it compiles properly on OS X, but it returns -1 with errno set to ENOSYS (function not implemented). Jun 4, 2011 at 4:04
  • Ok, now I'm looking for a good example of semaphores using C++ boost libraries. I have heard that is a robust implementation Aug 6, 2011 at 7:00
  • sem_getvalue() doesn't work either...... See stackoverflow.com/questions/16655153/…
    – user454322
    May 28, 2014 at 10:10
26

A better solution (these days) than named semaphores on OS X is Grand Central Dispatch's dispatch_semaphore_t. It works very much like the unnamed POSIX semaphores.

Initialize the semaphore:

#include <dispatch/dispatch.h>
dispatch_semaphore_t semaphore;
semaphore = dispatch_semaphore_create(1); // init with value of 1

Wait & post (signal):

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
...
dispatch_semaphore_signal(semaphore);

Destroy:

dispatch_release(semaphore);

The header file is well documented and I found it quite easy to use.

8
  • GCD semaphore is GCD level feature. Theoretically, it can cause some issue if combined with pthread level.
    – eonil
    Dec 24, 2016 at 17:31
  • @Eonil can you elaborate? What sort of issues could you have? Mar 29, 2019 at 17:28
  • 2
    @MichaelDorst Wrong question. Basically, you should not use API exposed on different abstraction to control another abstraction unless they are explicitly designed to work together. Even if you know all the details and they are working now because implementation details can change later. If you have no evidence of interoperability, you should assume such usage as unsafe due to potential issues. And there's no evidence that GCD API will work as expected for POSIX thread.
    – eonil
    Apr 4, 2019 at 16:32
  • @MichaelDorst Correct question would be "What's the evidence of GCD APIs would work for POSIX thread?" No evidence means unsafe.
    – eonil
    Apr 4, 2019 at 16:36
  • @MichaelDorst GCD is not an "extension" or "utility" feature to aid pthread. It's a separated and opaque abstraction that does not depend on implementation details.
    – eonil
    Apr 4, 2019 at 16:39
0

If you look at the implementation of sem_init in the source then it just returns an error, while some of the other bsd fns like sem_open still have implementation.

Both the "deprecated" posix fns and libdispatch/GCD call from userspace using fns like semphore_create and semaphore_wait. You can use these directly if you want an old-style sema that always uses the kernel/OS, but you're better with ones like from GCD that uses atomic counters internally and only calls the kernel/OS if it has to wait.

https://github.com/apple/darwin-xnu/blob/a1babec6b135d1f35b2590a1990af3c5c5393479/bsd/kern/posix_sem.c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.