waiting for multiple condition variables in boost?

后端 未结 5 1599
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 09:36

I\'m looking for a way to wait for multiple condition variables. ie. something like:

boost::condition_variable cond1;  
boost::condition_variable cond2;

void wa         


        
5条回答
  •  -上瘾入骨i
    2021-02-05 10:10

    I don't believe you can do anything like this with boost::thread. Perhaps because POSIX condition variables don't allow this type of construct. Of course, Windows has WaitForMultipleObjects as aJ posted, which could be a solution if you're willing to restrict your code to Windows synchronization primitives.

    Another option would to use fewer condition variables: just have 1 condition variable that you fire when anything "interesting" happens. Then, any time you want to wait, you run a loop that checks to see if your particular situation of interest has come up, and if not, go back to waiting on the condition variable. You should be waiting on those condition variables in such a loop anyways, as condition variable waits are subject to spurious wakeups (from boost::thread docs, emphasis mine):

    void wait(boost::unique_lock& lock)
    ...
    Effects:
    Atomically call lock.unlock() and blocks the current thread. The thread will unblock when notified by a call to this->notify_one() or this->notify_all(), or spuriously. ...

提交回复
热议问题