2011-02-05 60 views
0

所以我有一個共享併發隊列。它似乎很好地工作,除了銷燬。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(); 
    } 

}; 

回答

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 
} 
+0

其他線程都在等待上condition_variable ..他們仍然需要被喚醒,然後加入。這都需要排隊析構函數,然後意味着隊列需要跟蹤的所有它的工作..也許使用升壓線程內發生:thread_group會提出這樣的一個小整潔,雖然。 – 2011-02-05 04:37:56

+0

它看起來像有一個`cancel()`函數可以喚醒每個線程並拋出'Cancelled`異常。然後,每個線程都可以捕獲`Cancelled`異常並調用`return`。我誤解了你寫的'cancel()'嗎? – JaredC 2011-02-05 04:44:46

1

您有兩種選擇。當隊列超出範圍時,實際上並不會在其他線程引用時被銷燬(即使用shared_ptr,將其傳遞給其他線程;在main()的末尾調用cancel();一旦其他線程拋出已取消並可能退出,隊列將被破壞)。

或者,如果你想確保它的實際的main()的結束時被銷燬,那麼你就需要等待其他線程。如果您在處理析構函數之外的等待時可以執行JaredC的建議。要在析構函數內部完成它,看起來更清晰的是不要存儲所有的線程,而只是爲了保留一個計數和另一個同步原語。無論哪種方式,您都需要隊列來維持某種狀態,以等待所有線程完成。

對我來說,似乎第一個解決方案(與shared_ptr)更清潔。