Does a mutex get unlocked when calling notify on a condition variable?
Asked Answered
H

3

11

I am trying to understand what happens to a mutex when it is used in a condition variable.

In the following example, taken from cppreference

int main()
{
    std::queue<int> produced_nums;
    std::mutex m;
    std::condition_variable cond_var;
    bool done = false;
    bool notified = false;

    std::thread producer([&]() {
        for (int i = 0; i < 5; ++i) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::unique_lock<std::mutex> lock(m);
            std::cout << "producing " << i << '\n';
            produced_nums.push(i);
            notified = true;
            cond_var.notify_one();
        }   

        done = true;
        cond_var.notify_one();
    }); 

    std::thread consumer([&]() {
        std::unique_lock<std::mutex> lock(m);
        while (!done) {
            while (!notified) {  // loop to avoid spurious wakeups
                cond_var.wait(lock);
            }   
            while (!produced_nums.empty()) {
                std::cout << "consuming " << produced_nums.front() << '\n';
                produced_nums.pop();
            }   
            notified = false;
        }   
    }); 

    producer.join();
    consumer.join();
}

The producer thread calls cond_var.notify_one() before the mutex gets unlocked. Does the mutex m get unlocked when notify is called, or does the notification occurs only when the mutex gets unlocked?

Hosanna answered 22/10, 2012 at 20:33 Comment(0)
T
12

Notifying does not unlock the mutex. You can tell (indirectly) because you don't pass the lock to notify_one() the way you do to wait(), which does release the mutex while it waits.

On the other side, the notified thread(s) are notified "immediately". But they won't necessarily return from wait() immediately. Before they can return from wait() they must first re-acquire the mutex, so they will block there until the notifying thread releases it.

Tailspin answered 22/10, 2012 at 20:38 Comment(0)
R
3

The lock is being acquired in the constructor and released in the destructor of std::unique_lock. From this info you can deduce that the producer releases the lock after the call to notify_one() completes.

Relic answered 24/10, 2012 at 20:14 Comment(1)
exactly what is was looking for :DSosa
T
0

For performance reason I suggest to unlock the mutex before to notify other threads.

Tuberculosis answered 3/1, 2017 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.