Abstract


  • Limit the number of Process (进程)/Thread running on a particular resource at any time with a non-negative integer

Simultaneous Multi-Locking

  • Semaphore can be used to lock a resource at more than one Process (进程)/Thread at the same time
  • For example, maximum 5 database connections & online game queueing system

Decoupled Locking & Releasing

Code Snippets


  • sem_t semaphore; & sem_init(&semaphore, 0, 1);
  • Decrement the value when a Process (进程)/Thread running - sem_wait(&semaphore);
  • Increase the value when a Process (进程)/Thread finishes running - sem_post(&semaphore);
#include <semaphore.h>
 
int main() {
  sem_t semaphore;
  // 0 means it is not shared with other processes, 1 means only 1 thread can run at a time
  sem_init(&semaphore, 0, 1);
  
  sem_wait(&semaphore); // semmaphore--
  sem_post(&semaphore); // semmaphore++
  
  sem_destroy(&semaphore);
  return 0;
}