Link to home
Start Free TrialLog in
Avatar of farzanj
farzanjFlag for Canada

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.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of farzanj

ASKER

I am on it, I apologize for the delay but I was detained by some other priorities.  I need some more clarifications.  For instance:
If sleeping threads are rescheduled by themselves, why do we need notify and notify_all?
How do you do FIFO scheduling?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of farzanj

ASKER

Thank you!