Semaphore operations are the same in both Solaris and POSIX. The function name changed from sema_ in Solaris to sem_ in pthreads.
#include <thread.h> int sema_init(sema_t *sp, unsigned int count, int type, void *arg);
Use sema_init() to initialize the semaphore variable pointed to by sp by count amount. type can be one of the following (note that arg is currently ignored).
USYNC_PROCESS The semaphore can be used to synchronize threads in this process and other processes. Only one process should initialize the semaphore. arg is ignored.
USYNC_THREAD The semaphore can be used to synchronize threads in this process, only. arg is ignored.
Multiple threads must not initialize the same semaphore simultaneously. A semaphore must not be reinitialized while other threads may be using it.
#include <thread.h> sema_t sp; int ret; int count; count = 4; /* to be used within this process only */ ret = sema_init(&sp, count, USYNC_THREAD, 0);
#include <thread.h> sema_t sp; int ret; int count; count = 4; /* to be used among all the processes */ ret = sema_init (&sp, count, USYNC_PROCESS, 0);
#include <thread.h> int sema_post(sema_t *sp);
Use sema_post() to atomically increment the semaphore pointed to by sp. When any threads are blocked on the semaphore, one is unblocked.
#include <thread.h> int sema_wait(sema_t *sp);
Use sema_wait() to block the calling thread until the count in the semaphore pointed to by sp becomes greater than zero, then atomically decrement it.
#include <thread.h> int sema_trywait(sema_t *sp);
Use sema_trywait() to atomically decrement the count in the semaphore pointed to by sp when the count is greater than zero. This function is a nonblocking version of sema_wait().
#include <thread.h> int sema_destroy(sema_t *sp);
Use sema_destroy() to destroy any state associated with the semaphore pointed to by sp. The space for storing the semaphore is not freed.