A mutex is a locking mechanism with ownership: only the thread that locked it can unlock it, and it protects a critical section (value 0 or 1).
A semaphore is a signaling mechanism with a counter, manipulated by wait (P, decrement and block if zero) and signal (V, increment and wake a waiter); a counting semaphore allows up to N concurrent users, while a binary semaphore behaves like a lock but without ownership, so any thread can signal it.
Use a mutex for mutual exclusion and a semaphore for resource counting and for signaling between threads (such as producer and consumer).
Loading…