2012-05-10 1 views
1

私はブーストスレッドを使うことを学んでいます。私は両方のスレッドで利用可能なキューに数値を送り、ワーカースレッドに出力するシンプルなプログラムを作ろうとしています。私はそれを1ブーストスレッドが終了するのをメインが待たないようにしますか?

を養う場合、私は参加を呼び出すときに問題があるワーカースレッドがシャットダウンするように

が、私はそれを作った、私のメインスレッドはただそこに座って、終了するワーカースレッドを待ちます。これいらない。私はメインがワーカースレッドと並行して実行し続けるようにしたい。

template<typename Data> 
class concurrent_queue 
{ 
private: 
    std::queue<Data> the_queue; 
    mutable boost::mutex the_mutex; 
    boost::condition_variable the_condition_variable; 
public: 
    void push(Data const& data) 
    { 
     boost::mutex::scoped_lock lock(the_mutex); 
     the_queue.push(data); 
     lock.unlock(); 
     the_condition_variable.notify_one(); 
    } 

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

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

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

    void wait_and_pop(Data& popped_value) 
    { 
     boost::mutex::scoped_lock lock(the_mutex); 
     while(the_queue.empty()) 
     { 
      the_condition_variable.wait(lock); 
     } 

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

}; 

void workerFunc(concurrent_queue<int>* q) 
{ 
    while(true) 
    { 
     while(!q->empty()) 
     { 
      int p = -1; 
      q->wait_and_pop(p); 
      std::cout << p; 

      if(p == 1) 
      { 
       return; 
      } 
     } 
    } 
} 

int main(int argc, char* argv[]) 
{ 
    concurrent_queue<int> m_q; 
    std::cout << "main: startup" << std::endl; 

    boost::thread workerThread(workerFunc,&m_q); 

    std::cout << "main: waiting for thread" << std::endl; 

    m_q.push(6); 
    m_q.push(11); 
    workerThread.join(); 
    m_q.push(99); //will not reach here 
    std::cout << "main: done" << std::endl; 

    return 0; 
} 

それはメインスレッドにより、いつでも与えられることができることをいくつかを持っているときおかげ

私はスレッドがアクティブで実行しているだけのプロセス番号になりたいです。

+2

が完了するスレッドオブジェクトの定義が待機により、参加します。メインスレッドを待機させるまで、スレッドに参加しないでください。 – Chad

+0

スレッドをアクティブにして実行し、メインスレッドがいつでも与えることができるスレッド数がある場合にのみ処理します。 – jmasterx

+0

次に私の答えを見てください。 – Chad

答えて

2

join()機能は、接続されたスレッドが完了するのを待つ機能です。メインスレッドがjoin()を呼び出す理由はです。プログラムの稼動中にスレッドを実行し続けることが目的の場合(または1をキューに入れるまで)です。

join()そのスレッドをまったく使用しないと、workerThreadオブジェクトが有効範囲外になるとスレッドが切り離されます。これはほとんどあなたが望むものではありません。通常、マルチスレッドプログラムは、すべてのスレッドをmainから返す前にjoin()します。あなたの場合、join()に電話をかける前に、プログラムを正常終了させるために、あなたのスレッドに特別な信号を送る必要があります(1をあなたのキューに押し込む必要があります)。ここで

は例です:

int main(int argc, char* argv[]) 
{ 
    concurrent_queue<int> m_q; 
    std::cout << "main: startup" << std::endl; 

    boost::thread workerThread(workerFunc,&m_q); 

    std::cout << "main: waiting for thread" << std::endl; 

    m_q.push(6); 
    m_q.push(11); 

    // not ready for this yet... 
    // workerThread.join(); 

    m_q.push(99); 

    boost::this_thread::sleep(1000); 

    m_q.push(50); 
    m_q.push(30); 

    std::cout << "main: done" << std::endl; 

    m_q.push(1); 
    workerThread.join(); 

    return 0; 
} 
+0

しかし、あなたがそれをやっているところでは、労働者が働いている間、メインは決して仕事をすることができません。それが私の目標です。私のプロジェクトでは、メインスレッドがクライアントからのメッセージを受信し、それらのメッセージを適切なスレッドにディスパッチさせます。 – jmasterx

+0

そうでなければ、メインはものを行い、ワーカーはものを行い、メインはものをやります...そして、それは私が望むものではありません。 – jmasterx

+0

できます。メインは、それが望むような仕事を得ることができます。ワーカースレッドとは完全に非同期です。 'sleep'をユーザの入力や他の何かを集めることで置き換えることができます。 – Chad

関連する問題