POSIX Thread and Semaphore on Darwin and Cygwin

Darwin (here I mean Mac OS X up to 10.6.5) supports pthread and semaphore.  However, it is notable that only named semaphores (sem_open and sem_close) are supported, unnamed semaphores (sem_init and sem_destroy) are not.

Cygwin supports full pthread and semaphore.

To make your program compatible with both platforms, use the following macros (http://lists.apple.com/archives/darwin-kernel/2005/Dec/msg00022.html):

#include <unistd.h>

#if ((_POSIX_SEMAPHORES - 200112L) >= 0)
/* This platform fully supports POSIX semaphores */
#if defined(__APPLE__)
/* This platform can support POSIX named semaphores, but not unnamed semaphores */
/* this platform requires that I Google code for P/V semaphores and include it here */

Also, you need

#include <pthread.h>  // for POSIX thread mutex and condition variables
#include <semaphore.h>  // for POSIX semaphore

On both platforms, you can use g++ -lpthread to build programs using POSIX thread and semaphore.