Question

Mutexes are used to protect critical sections. Let's say a down has been already done on a mutex, and while the thread that did that is in the CS, 10 other threads are right behind it and do a down on the mutex, putting themselves to sleep. When the first thread exits the critical section and does an up on the mutex, do all 10 threads wake up and just resume what they were about to do, namely, entering the critical section? Wouldn't that mean then that all 10 might end up in the critical section at the same time?

Was it helpful?

Solution

No, only one thread will wake up and take ownership of the mutex. The rest of them will remain asleep. Which thread is the one that wakes up is usually nondeterministic.

The above is a generalisation and the details of implementation will be different in each system. For example, in Java compare Object#notify() and Object#notifyAll().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top