升压条件变量 - 执行调用" notify_one"栈? [英] Boost condition variables - do calls to "notify_one" stack?

查看:154
本文介绍了升压条件变量 - 执行调用" notify_one"栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

消费者线程在一个生产者使用Boost线程/单个消费者的应用程序,会发生什么,如果生产者线程使多个调用 cond_var.notify_one()前名为 cond_var.wait(锁定)

In a single producer / single consumer application using Boost threads, what happens if the producer thread makes more than one call to cond_var.notify_one() before the consumer thread has called cond_var.wait(lock) ?

将notify_one到额外调用堆叠,使得每个调用 .wait()将对应1 :1以 .notify_one()致电

Will the additional calls to notify_one be stacked, such that each call to .wait() will correspond 1:1 with a .notify_one() call?

修改 A <一个href=\"http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html\"相对=nofollow>普遍引用的例子,以实现并发队列中有以下方法:

EDIT A commonly quoted example for implementing a concurrent queue has these methods:

void push(Data const& data)
{
    boost::mutex::scoped_lock lock(the_mutex);
    the_queue.push(data);
    lock.unlock();
    the_condition_variable.notify_one();
}

void wait_and_pop(Data& popped_value)
{
    boost::mutex::scoped_lock lock(the_mutex);
    while(the_queue.empty())
    {
        the_condition_variable.wait(lock);
    }

    popped_value=the_queue.front();
    the_queue.pop();
}

我已经使用了一些非常类似code和经历这似乎由消费者线程没有醒来,每 .notify_one()(因为它仍然忙于做其他工作),并想知道是否缺乏叠加可能是原因。

I've used some very similar code, and experienced some odd memory growth which appears to be explained by the consumer thread not waking up for every .notify_one() (because it's still busy doing other work), and wondered whether lack of "stacking" might be the cause.

这似乎是没有这种堆叠code如果(有时)消费者线程不能与生产者线程跟不上会失败。如果我的理论是正确的,我想就如何解决这个问题code AP preciate建议。

It would seem that without stacking this code would fail if (on occasion) the consumer thread cannot keep up with the producer thread. If my theory is correct, I'd appreciate suggestions on how to remedy this code.

推荐答案

的notify_one 该规范是:

C ++ 11 30.5.1 / 7:效果:如果任何线程被阻塞等待 *本,疏导那些theads之一

C++11 30.5.1/7: Effects: If any threads are blocked waiting for *this, unblocks one of those theads.

因此​​,答案是否定的:调用 notify_one notify_all 将才醒悟线程(S)目前在等待,并且不记得以后。

So the answer is no: calls to notify_one and notify_all will only wake thread(s) currently waiting, and are not remembered for later.

更新:对不起,我误解了一个问题,的std :: condition_variable 。毫不奇怪,<一个href=\"http://www.boost.org/doc/libs/release/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref.condition_variable.notify_one\">Boost规范是或多或少相同

UPDATE: Sorry, I misread the question as std::condition_variable. Not surprisingly, the Boost specification is more or less identical:

如果任何线程当前的阻止的等待 *本在调用 TIMED_WAIT ,疏导这些线程之一。

If any threads are currently blocked waiting on *this in a call to wait or timed_wait, unblocks one of those threads.

关于您的编辑:如果没有线程,当有人呼叫等待,那么下一个线程调用弹出将不等待,因为 the_queue 将不能为空。因此,有没有必要的条件变量要记住,它不应该等待;该信息被存储在状态被管理,而不是状态变量。如果消费者不能与生产跟不上,那么你需要或者加快形成消费,或放慢生产;没有什么可以做,以信令机制,以帮助这一点。

Regarding your edit: If there is no thread waiting when someone calls push, then the next thread to call pop won't wait at all, since the_queue will not be empty. So there's no need for the condition variable to remember that it shouldn't wait; that information is stored in the state being managed, not the condition variable. If the consumer can't keep up with the producer, then you need to either speed up consumption or slow down production; there's nothing you can do to the signalling mechanism to help with that.

这篇关于升压条件变量 - 执行调用&QUOT; notify_one&QUOT;栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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