条件Vairable和notify_all

时间:2018-03-16 17:25:49

标签: c++ multithreading

在下面的代码中(取自添加了额外cout的cpp引用)为什么我们在第一个...finished waiting.之后看不到cv.notify_all

#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>

std::condition_variable cv;
std::mutex cv_m; // This mutex is used for three purposes:
                 // 1) to synchronize accesses to i
                 // 2) to synchronize accesses to std::cerr
                 // 3) for the condition variable cv
int i = 0;

void waits()
{
    std::unique_lock<std::mutex> lk(cv_m);
    std::cerr << "Waiting... \n";
    cv.wait(lk, []{return i == 1;});
    std::cerr << "...finished waiting. i == 1\n";
}

void signals()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
        std::lock_guard<std::mutex> lk(cv_m);
        std::cerr << "Notifying...\n";
    }
    cv.notify_all();

    std::cerr << "I should see i here...\n";

    std::this_thread::sleep_for(std::chrono::seconds(1));

    {
        std::lock_guard<std::mutex> lk(cv_m);
        i = 1;
        std::cerr << "Notifying again...\n";
    }
    cv.notify_all();
}

int main()
{
    std::thread t1(waits), t2(waits), t3(waits), t4(signals);
    t1.join(); 
    t2.join(); 
    t3.join();
    t4.join();
}

输出:

Waiting... 
Waiting... 
Waiting... 
Notifying...
I should see i here...
Notifying again...
...finished waiting. i == 1
...finished waiting. i == 1
...finished waiting. i == 1

2 个答案:

答案 0 :(得分:3)

因为这一行:

cv.wait(lk, []{return i == 1;});

你传递了一个谓词,因此只有在谓词返回true时它才会被唤醒。在第一个notify_all()之后,谓词不满意。

答案 1 :(得分:1)

因为没有等完。

你告诉它要等到i 1,但它仍然是0。所以它会一直等待。

即使您“通知”它也是如此 - 您通知它发生了更改,但实际上更改没有发生,所以它又回到了等待状态。

notify()不是ignore_the_predicate_i_gave_you_and_stop_waiting()