previous | start | next

Semaphore Solution

We need 2 semaphores, one for each synchronization requirement.

We need 1 mutex.

Assume the buffer size is N items.

// shared
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
sem_t full_slot;
sem_t empty_slot;

sem_init(&full_slot, 0, 0);
sem_init(&empty_slot, 0, N);


previous | start | next