Search code examples
cpthreadssemaphoreproducer-consumer

pthread API equivalents of semaphores in semaphore.h


This question is related to a solution to the producer/consumer problem I'm working on. We were asked to use pthreads to solve the problem. In order to create my empty and full semaphores for the bounded buffer, I used semaphore.h, but I was told there is a method for accomplishing this without semaphore.h.

Is there a standard method for creating semaphores using the pthread API? That is, is there a simple equivalent to the following using pthreads only:

sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
sem_wait(&empty);
sem_post(&full);
etc.

Thanks for any info you can give me on this topic.


Solution

  • The <semaphore.h> functions (sem_init() / sem_open() etc. ) are the pthreads semaphore API.

    Perhaps you are being guided towards using condition variables instead of sempahores to solve this problem?