C++ C+中的多线程队列销毁+;
所以我有一个共享的并发队列。它似乎运行良好,除了破坏 队列的实现方式是它包含一个条件变量和互斥对。 启动了几个等待此条件变量的工作线程。当新对象可用于处理时,会将其推入队列,并向条件变量发送信号 问题是,当主线程退出并销毁队列时,条件变量将被销毁,但由于条件变量正在使用,这将失败。这引发了一个异常,一切都变得糟糕透顶 我想给工人们发个信号,叫醒他们,让他们离开,等待他们完成,然后继续主线。我的问题是,当这些线程完成时,我需要一个额外的同步原语吗 无论如何,以下是队列的代码:C++ C+中的多线程队列销毁+;,c++,multithreading,queue,C++,Multithreading,Queue,所以我有一个共享的并发队列。它似乎运行良好,除了破坏 队列的实现方式是它包含一个条件变量和互斥对。 启动了几个等待此条件变量的工作线程。当新对象可用于处理时,会将其推入队列,并向条件变量发送信号 问题是,当主线程退出并销毁队列时,条件变量将被销毁,但由于条件变量正在使用,这将失败。这引发了一个异常,一切都变得糟糕透顶 我想给工人们发个信号,叫醒他们,让他们离开,等待他们完成,然后继续主线。我的问题是,当这些线程完成时,我需要一个额外的同步原语吗 无论如何,以下是队列的代码: // Based o
// 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();
}
};
//基于http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
//安东尼·威廉姆斯原版
//迈克尔·安德森的修改
#包括“boost/thread.hpp”
#包括
模板
类并发队列
{
私人:
std::确定_队列;
mutable boost::mutex the_mutex;
boost::条件变量\u条件变量;
布尔被取消;
公众:
并发_队列():_队列()、_互斥体()、_条件_变量()已_取消(false){}
结构取消{};
无效推送(数据常量和数据)
{
boost::mutex::作用域的_锁锁(_互斥锁);
如果(被取消)抛出已取消();
队列。推回(数据);
lock.unlock();
_condition_变量。notify_one();
}
bool empty()常量
{
boost::mutex::作用域的_锁锁(_互斥锁);
如果(被取消)抛出已取消();
返回_queue.empty();
}
bool try_pop(数据和弹出值)
{
boost::mutex::作用域的_锁锁(_互斥锁);
如果(被取消)抛出已取消();
if(_queue.empty())
{
返回false;
}
popped_value=_queue.front();
队列。pop_front();
返回true;
}
无效等待和弹出(数据和弹出值)
{
boost::mutex::作用域的_锁锁(_互斥锁);
while(队列.empty()&&!已取消)
{
_条件_变量wait(lock);
}
如果(被取消)抛出已取消();
popped_value=_queue.front();
队列。pop_front();
}
等待和接受一切
{
boost::mutex::作用域的_锁锁(_互斥锁);
while(队列.empty()&&!已取消)
{
_条件_变量wait(lock);
}
如果(被取消)抛出已取消();
std::deque retval;
std::swap(retval,_队列);
返回返回;
}
作废取消()
{
boost::mutex::作用域的_锁锁(_互斥锁);
如果(被取消)抛出已取消();
is_=true;
lock.unlock();
_condition_变量。notify_all();
}
};
您可以在每个线程上调用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\u 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
}
intmain()
{
boost::线程组tg;
对于(int i=0;i<10;++i)
tg.创建_螺纹和销;
//停止工作的信号
加入所有人;
//销毁队列
}
您可以在每个线程上调用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\u 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
}
intmain()
{
boost::线程组tg;
对于(int i=0;i<10;++i)
tg.创建_螺纹和销;
//停止工作的信号
加入所有人;
//销毁队列
}
您有两个选择。当队列超出作用域时,它实际上不会被销毁,而其他线程引用它(即,使用shared_ptr,将其传递给其他线程;在main()末尾调用cancel();一旦其他线程抛出Cancelled并可能退出,队列将被销毁)
或者,如果您想确保它实际上被main()的末尾销毁,那么您需要等待其他线程。如果您对在析构函数外部处理等待没有问题,那么可以按照JaredC的建议执行。要在析构函数中执行此操作,似乎不需要存储所有线程,而只需保留一个计数和另一个同步原语。无论哪种方式,您都需要队列保持某种状态,以允许它等待所有线程完成
在我看来,第一种解决方案(使用共享的ptr)更干净。您有两种选择。当队列超出作用域时,它实际上不会被销毁,而其他线程引用它(即,使用shared_ptr,将其传递给其他线程;在main()末尾调用cancel();一旦其他线程抛出Cancelled并可能退出,队列将被销毁) 或者,如果您想确保它实际上被main()的末尾销毁,那么您需要等待其他线程。如果您对在析构函数外部处理等待没有问题,那么可以按照JaredC的建议执行。要在析构函数中执行此操作,似乎不需要存储所有线程,而只需保留一个计数和另一个同步原语。无论哪种方式,您都需要队列保持某种状态,以允许它等待所有线程完成
在我看来,第一个解决方案(使用shared_ptr)似乎更干净。其他线程都在等待condition_变量。。他们仍然需要被唤醒,然后加入。这一切都需要发生在队列析构函数内部,这意味着队列需要跟踪在其上工作的所有线程。。也许使用boost:thread_组会使它更整洁一些。它