C ++中的多线程队列破坏

时间:2011-02-05 04:19:23

标签: c++ multithreading queue

所以我有一个共享的并发队列。除了破坏之外,它看起来效果很好。

实现队列的方式是它包含一个条件变量和互斥对。 启动了几个工作线程,等待这个条件变量。当新对象可用于处理时,它们被推入队列并且条件变量发出信号。

问题是当主线程退出时,破坏队列,条件变量被销毁,但是当条件变量正在使用时,这会失败。这引发了一个例外,一切都被彻底打破了。

我想向工人发出信号,唤醒他们并让他们退出,并等待他们完成,然后继续主线程。当这些线程完成时我的问题是解决 - 我是否需要一个额外的同步原语?

无论如何都是队列的代码:

// Based on code from http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
// Original version by Anthony Williams
// Modifications by Michael Anderson

#include "boost/thread.hpp"
#include <deque>

template<typename Data>
class concurrent_queue
{
private:
    std::deque<Data> the_queue;
    mutable boost::mutex the_mutex;
    boost::condition_variable the_condition_variable;
    bool is_canceled;

public:
    concurrent_queue() : the_queue(), the_mutex(), the_condition_variable(), is_canceled(false) {}
    struct Canceled{};
    void push(Data const& data)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        if (is_canceled) throw Canceled();
        the_queue.push_back(data);
        lock.unlock();
        the_condition_variable.notify_one();
    }

    bool empty() const
    {
        boost::mutex::scoped_lock lock(the_mutex);
        if (is_canceled) throw Canceled();
        return the_queue.empty();
    }

    bool try_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        if (is_canceled) throw Canceled();
        if(the_queue.empty())
        {
            return false;
        }

        popped_value=the_queue.front();
        the_queue.pop_front();
        return true;
    }

    void wait_and_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);

        while(the_queue.empty() && !is_canceled)
        {
            the_condition_variable.wait(lock);
        }
        if (is_canceled) throw Canceled();

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

    std::deque<Data> wait_and_take_all()
    {
        boost::mutex::scoped_lock lock(the_mutex);

        while(the_queue.empty() && !is_canceled)
        {
            the_condition_variable.wait(lock);
        }
        if (is_canceled) throw Canceled();

        std::deque<Data> retval;
        std::swap(retval, the_queue);
        return retval;
    }

    void cancel()
    {
       boost::mutex::scoped_lock lock(the_mutex);
       if (is_canceled) throw Canceled();
       is_canceled = true;
       lock.unlock();
       the_condition_variable.notify_all();
    }

};

2 个答案:

答案 0 :(得分:2)

您可以在每个线程上调用join()等待它完成执行。像这样:

void DoWork() {};

int main()
{
    boost::thread t(&DoWork);
    // signal for the thread to exit
    t.join();    // wait until it actually does exit

    // destroy the queue
}

或者您可以将boost::thread_group用于多个线程。

int main()
{
    boost::thread_group tg;

    for(int i = 0 ; i < 10 ; ++i)
        tg.create_thread(&DoWork);

    // signal to stop work

    tg.join_all();

    // destroy the queue
}

答案 1 :(得分:1)

您有两种选择。当队列超出范围时,它实际上并没有被破坏,而其他线程引用它(即使用shared_ptr,将其传递给其他线程;在main()结束时调用cancel();一旦其他线程throw取消并且可能退出,队列将被销毁)。

或者如果你想确保它在main()的末尾被实际销毁,那么你需要等待其他线程。你可以做JaredC建议的,如果你可以处理析构函数之外的等待。要在析构函数中执行此操作,不要存储所有线程,只是为了保持计数和另一个同步原语似乎更干净。无论哪种方式,您都需要队列来维持某种状态,以允许它等待所有线程完成。

对我而言,似乎第一个解决方案(使用shared_ptr)更清晰。