C 线程中的notify_one()醒来多个线程

notify_one() in c++ thread waking up more than one thread

本文关键字:线程 醒来 notify one      更新时间:2023-10-16

你好,我有以下代码:

// condition_variable example
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void print_id (int id) {
  std::unique_lock<std::mutex> lock(mtx);
  while (!ready) cv.wait(lock);
  // ...
  std::cout << "thread " << id << std::endl;
}
void go() {
  std::unique_lock<std::mutex> lock(mtx);
  ready = true;
  cv.notify_one();
}
int main ()
{
  std::thread threads[10];
  // spawn 10 threads:
  for (int i=0; i<10; ++i)
    threads[i] = std::thread(print_id,i);
  std::cout << "10 threads ready to race..." << std::endl;
  go();                       // go!
  for (auto& th : threads) th.join();
  std::cout << "Finished!" << std::endl;
  return 0;
}

这是输出:

10 threads ready to race...
thread 9
thread 0

我的期望是,通过调用notify_one(),只会通知一个线程,我将陷入僵局。但是在这种情况下,在陷入僵局之前通知了两个线程。我在这里想念什么?谢谢

可能是在调用go()时启动所有线程。然后这可能会发生:

  1. 九个线程正在运行并等待条件变量。
  2. 您调用将ready设置为true并通知线程零的go()
  3. 启动了第十个线程,看到readytrue,不等待条件变量。

您不应指望通知线程的数量。notify_one醒来"至少一个,如果存在等待线程"。

C 标准允许虚假唤醒,这意味着无通知醒来的线程。

相反,请检查您在静音中保护的变量。如果需要确切的通知行为,则可以考虑添加计数器。