sem_init on OS X sem_init on OS X c c

sem_init on OS X


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.


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.