farzanj
asked on
Mutex, lock, wait, notify mechanism
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
ASKER
If sleeping threads are rescheduled by themselves, why do we need notify and notify_all?
How do you do FIFO scheduling?