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);