是否有任何以下C ++阻塞条件变量

时间:2010-02-23 05:05:40

标签: c++ multithreading

我希望有以下阻止条件变量

  1. 一旦发出阻塞条件变量信号,所有线程都不会等待特定条件变量
  2. 条件变量可以随时取消信号。
  3. 一旦条件变量没有信号,所有线程都将等待变量
  4. 我看http://www.boost.org/doc/libs/1_34_0/doc/html/boost/condition.html

    但是,它在(1)

    中不起作用

    好像没有线程等待条件变量,被调用的notify_one / notify_all会使信号丢失

    1) thread TA try wait on condition variable C
    2) thread TB call `notify_all`
    3) thread TA will continue execution
    

    1) thread TB call `notify_all`
    2) thread TA wait on condition variable C
    3) thread TA will still continue waiting  <-- How can I have condition variable C remains in
                                                  signaled state, and make no wait for thread TA
    

2 个答案:

答案 0 :(得分:1)

您的条件变量必须与必须检查的布尔值(等待)结合使用。等待错误并通知所有人。所以任何没有等待的线程都应检查wait-variable并继续 当然,线程只有在到达条件变量代码时才会再次等待。

答案 1 :(得分:0)

解决方案是手动重置事件如下:

CreateEvent(0, TRUE, TRUE, 0);

一旦通过SetEvent发出信号,它将始终处于信号状态。