使用boost線程的一個簡單的例子

//需要使用-lboost_thread鏈接thread庫

#include "boost/thread/mutex.hpp"
#include "boost/thread/thread.hpp"
#include "boost/thread/lock_guard.hpp"
#include <iostream>

using namespace std;
using namespace boost;

class A
{
public:
 static void print(std::string s)
 {
  boost::unique_lock<boost::mutex> lock(mutex_);
  for (int j = 0;j<10;++j)
  {
   cout << ++i <<endl;
  }
 }
protected:
private:
 static boost::mutex mutex_;
 static int i;
};

int A::i = 0;
boost::mutex A::mutex_;


int main()
{
 boost::thread t1(A::print,"hello world");
 t1.join();
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章