11月 13, 2011

あるクラスのメンバ関数から他のメンバ関数をスレッドとして並列実行

C++でプログラムを書いているときに,クラスの中でスレッドを実行して並列処理を行いたくなった。
Boostという便利なライブラリを使って,これを実現します。

#include <boost/thread.hpp>
#include <boost/bind.hpp>
class hoge{
  private:
    thr_func(int a);
    func(){
      int b;
      boost::thread thr(boost::bind(&hoge::func, this, b); 
      thr.join();
    }
};
Boostの使い方とかはここを参照