C ++ std条件变量覆盖了很多共享变量 [英] C++ std condition variable covering a lot of share variables

查看:138
本文介绍了C ++ std条件变量覆盖了很多共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编写一个多线程程序,在我的主线程中,我正在等待我的其他线程将程序包放入不同的队列。取决于包的种类和它们来自哪个线程。

I am writing a multithreaded program in C++ and in my main thread I am waiting for my other threads to put packages in different queues. Depending on the sort of package and from which thread they originate.

队列受到互斥体的保护,因为它应该是。

The queues are protected by mutexes as it should be.

但在我的主要我不想做:

But in my main I don't want to be doing:

while(true)
if(!queue1->empty)
{
     do stuff
}
if(!queue2->empty)
{
     do stuff
}
etc

因此,您需要使用条件变量来指示主已经改变。现在我只能阻塞一个条件变量,所以我需要所有这些线程使用相同的条件变量和一个伴随的互斥。现在我不想真的使用这个互斥锁定我所有的线程。这并不意味着当1个线程写入队列时,另一个线程写入一个完全不同的队列。所以我对每个队列使用单独的互斥体。但现在我如何使用这个额外的互斥体与条件变量。

So you need to use condition variables to signal the main that something has changed. Now I can only block on 1 condition variable so I need all these threads to use the same conditional variable and an accompanying mutex. Now I dont want to really use this mutex to lock all my threads. It doesn't mean that when 1 thread is writing to a queue, another cant write to a totally different queue. So I use seperate mutexes for each queue. But now how do I use this additional mutex that comes with the conditional variable.

如何使用2个线程和1个队列使用boost,非常simular到std。
http:/ /www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

How it's done with 2 threads and 1 queue using boost, very simular to std. http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html :

template<typename Data>
class concurrent_queue
{
    private:
    boost::condition_variable the_condition_variable;  
    public:
    void wait_for_data()
    {
        boost::mutex::scoped_lock lock(the_mutex);
        while(the_queue.empty())
        {
             the_condition_variable.wait(lock);
        }
    }
    void push(Data const& data)
    {
         boost::mutex::scoped_lock lock(the_mutex);
         bool const was_empty=the_queue.empty();
         the_queue.push(data);
         if(was_empty)
         {
             the_condition_variable.notify_one();
         }
     }
     // rest as before
 };

那么如何解决这个问题呢?

So how do you solve this?

推荐答案

我会说你的问题的关键在这里:

I'd say the key to your problem is here:

现在我不想真的使用此互斥锁定所有我的线程。这并不意味着当1个线程写入队列时,另一个线程写入一个完全不同的队列。所以我为每个队列使用单独的互斥体。

为什么?因为:

...软件包比较慢。和大多数时候队列是空的

在我看来,你已经设计自己在一个角落,因为你认为你需要实际上你可能不需要它,因为一个队列实际上在你提到的使用场景中工作。

It seems to me that you've designed yourself into a corner because of something you thought you needed when in reality you probably didn't need it because one queue would have actually worked in the usage scenario you mention.

我想说,从一个队列开始,看看多远它得到你。然后,当你遇到一个限制,你真的有很多线程等待一个单一的互斥,你将有更多的信息,因此将能够更好地解决这个问题。

I'd say start off with one queue and see how far it gets you. Then, when you run into a limitation where you really do have many threads waiting on a single mutex, you will have a lot more information about the problem and will therefore be able to solve it better.

实质上,我会说你面对这个问题的原因是未成熟的设计优化,解决这个问题的方法是回溯跟踪和改变现在的设计。

In essence, I'd say the reason you're facing this problem is premature design optimization and the way to fix that would be to back track and change the design right now.

这篇关于C ++ std条件变量覆盖了很多共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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