Mutex, lock, wait, notify mechanism

Posted on 2012-09-21
Last Modified: 2012-10-05
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.
Question by:farzanj
    LVL 85

    Accepted Solution

    Correct.  The second thread will go to sleep and then wake up when the first releases the lock.

    For multiple threads, all will go to sleep.  Which one wakes up, though, is unspecified!  In other words, you can't rely on any specific ordering in the way they are woken up in turn.

    See "Scheduling Policies":

        "In order to ensure portability, all Boost.Threads mutex objects use an unspecified scheduling policy."

    This can lead to "thread starvation":

    In a nutshell, if you find this is a possibility for your current implementation, then you need to re-design your system with a more sophisticated scheduling algorithm.
    LVL 31

    Author Comment

    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?
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    notify tells the waiting thread(s) that some condition has been met, so they can wake up.  It is undefined, though, which waiting thread will actually get the lock and execute though.  If you notify_all, only one will actually acquire the lock at a time.

    This one is a good read:
    LVL 31

    Author Closing Comment

    Thank you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    "Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now