所以我有一個共享併發隊列。它似乎很好地工作,除了銷燬。C++中的多線程隊列銷燬
隊列實現的方式是它包含一個條件變量和互斥對。 幾個工作線程啓動,等待這個條件變量。當新對象可用於處理時,它們被推入隊列並且條件變量被髮送。
問題是,當主線程退出時,銷燬隊列,條件變量將被銷燬,但是當條件變量正在使用時會失敗。這引發了一個異常,並且所有事情都很快就爆發了。
我想指示工人,將他們喚醒並讓他們退出,等待他們完成,然後繼續執行主線程。我的問題是當這些線程完成時 - 我是否需要額外的同步原語?
反正繼承人隊列代碼:
// 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();
}
};
其他線程都在等待上condition_variable ..他們仍然需要被喚醒,然後加入。這都需要排隊析構函數,然後意味着隊列需要跟蹤的所有它的工作..也許使用升壓線程內發生:thread_group會提出這樣的一個小整潔,雖然。 – 2011-02-05 04:37:56
它看起來像有一個`cancel()`函數可以喚醒每個線程並拋出'Cancelled`異常。然後,每個線程都可以捕獲`Cancelled`異常並調用`return`。我誤解了你寫的'cancel()'嗎? – JaredC 2011-02-05 04:44:46