Brain Dump

Semaphore

Tags
comp-arch

Is a synchronisation primitive much like a Mutex, except instead of being a binary locked or unlocked, it counts down from some upper limit whenever it's acquired and blocks the calling thread when you try to acquire it while the current count is 0.

#include <semaphore.h>

sem_t s;
sem_init(&s, 0, 10);

sem_wait(&s); // Decrement the semaphore.
// ...
sem_post(&s); // Increment the semaphore.

sem_destroy(&s);
Code Snippet 1: Outline of the general usage of a semaphore with the semaphore library.

Links to this note