Mutex, lock, wait, notify mechanism
Posted on 2012-09-21
I am reading a lot of material but still not sure how it works. Question is about Boost threads or C++11 threads.
When a critical section is locked with scoped_lock (unique_lock) or lock_guard and suppose another thread tried to lock it while it is still locked, will the second thread go to sleep state ? And will it go to read state immediately after lock is released (first thread goes out of scope)?
Now if multiple threads try to lock an already locked thread, do they all go to sleep state? And when the lock is released, which one of the waiting threads go to ready state? All of them?
Why do we need notify_one? To wake up the first thread? Is there a queue of waiting threads?
Some sample code to do it efficiently would help.