等待升压多种条件变量? [英] waiting for multiple condition variables in boost?

查看:94
本文介绍了等待升压多种条件变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来等待多个条件变量。
IE浏览器。是这样的:

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

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

void wait_for_data_to_process()  
{  
    boost::unique_lock<boost::mutex> lock(mut);

    wait_any(lock, cond1, cond2); //boost only provides cond1.wait(lock);

    process_data();
}

是这样的事情可能的条件变量。如果不是有没有其他解决办法?

Is something like this possible with condition variables. And if not are there alternative solutions?

感谢

推荐答案

我不相信你可以做这样的事与升压::线程。也许是因为POSIX的条件变量不容许这种类型的构造。当然,Windows已经为AJ发布,这可能是一个解决方案,如果你愿意限制你的code到Windows同步原语WaitForMultipleObjects的。

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.

另一种选择是使用更少的条件变量:刚才有你火的时候,什么有趣的情况1条件变量。然后,你要等待一些时间,你运行一个循环,检查,看看是否感兴趣您的具体情况已经上来了,如果没有,回去等待条件变量。你应该在这样一个循环的条件变量反正等着,作为条件变量等待受虚假唤醒(从boost ::螺纹文档,重点煤矿):

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):

无效的等待(的boost :: unique_lock&LT;提高::互斥&GT;&安培;锁)

...

效果:

原子电话 lock.unlock()并阻止当前线程。 notify_one()或这个 - &GT; notify_all()这个 - &GT线程将解除>或不合逻辑。 ...

void wait(boost::unique_lock<boost::mutex>& 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. ...

这篇关于等待升压多种条件变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆