sem_t s; sem_init(&s, 0, B); // initialize s to value B sem_wait(&s); ... sem_post(&s);
For sem_init, the second parameter 0 means the semaphore is only used by threads in the same process. 1, means by threads in multiple processes.