1. 程式人生 > >boost::thread的六種使用方法總結

boost::thread的六種使用方法總結

boost::thread有兩個建構函式: 
(1)thread():構造一個表示當前執行執行緒的執行緒物件; 
(2)explicit thread(const boost::function0<void>& threadfunc): 

     boost::function0<void>可以簡單看為:一個無返回(返回void),無引數的函式。這裡的函式也可以是類過載operator()構成的函式;該建構函式傳入的是函式物件而並非是函式指標,這樣一個具有一般函式特性的類也能作為引數傳入,在下面有例子。

第一種情況:

#include <boost/thread/thread.hpp> 
#include <iostream> 
  
void hello() 
{ 
        std::cout << 
        "Hello world, I''m a thread!" 
        << std::endl; 
} 
  
int main(int argc, char* argv[]) 
{ 
        boost::thread thrd(&hello); 
        thrd.join(); 
        return 0; 
}

第二種情況:類過載operator()構成的函式建立執行緒

#include <boost/thread/thread.hpp> 
#include <boost/thread/mutex.hpp> 
#include <iostream> 
  
boost::mutex io_mutex; 
  
struct count 
{ 
        count(int id) : id(id) { } 
        
        void operator()() 
        { 
                for (int i = 0; i < 10; ++i) 
                { 
                        boost::mutex::scoped_lock 
                        lock(io_mutex); 
                        std::cout << id << ": " 
                        << i << std::endl; 
                } 
        } 
        
        int id; 
}; 
  
int main(int argc, char* argv[]) 
{ 
        boost::thread thrd1(count(1)); 
        boost::thread thrd2(count(2)); 
        thrd1.join(); 
        thrd2.join(); 
        return 0; 
} 

第三種情況:在類內部對static函式建立執行緒

#include <boost/thread/thread.hpp>
#include <iostream> 
class HelloWorld
{
public:
 static void hello()
 {
      std::cout <<
      "Hello world, I''m a thread!"
      << std::endl;
 }
 static void start()
 {
  
  boost::thread thrd( hello );
  thrd.join();
 }
 
}; 
int main(int argc, char* argv[])
{
 HelloWorld::start();
 
 return 0;
} 
在這裡start()和hello()方法都必須是static方法。

第四種情況:使用boost::bind函式建立執行緒

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream> 
class HelloWorld
{
public:
 void hello()
 {
    std::cout <<
    "Hello world, I''m a thread!"
    << std::endl;
 }
 void start()
 {
  boost::function0< void> f =  boost::bind(&HelloWorld::hello,this);
  //或boost::function<void()> f = boost::bind(&HelloWorld::hello,this);
  boost::thread thrd( f );
  thrd.join();
 }
 
}; 
int main(int argc, char* argv[])
{
 HelloWorld hello;
 hello.start();
 return 0;
}

第五種情況:在Singleton模式內部建立執行緒

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream> 
class HelloWorld
{
public:
 void hello()
 {
    std::cout <<
    "Hello world, I''m a thread!"
    << std::endl;
 }
 static void start()
 {
  boost::thread thrd( boost::bind  
                   (&HelloWorld::hello,&HelloWorld::getInstance() ) ) ;
  thrd.join();
 }
 static HelloWorld& getInstance()
 {
  if ( !instance )
      instance = new HelloWorld;
  return *instance;
 }
private: 
 HelloWorld(){}
 static HelloWorld* instance;
 
}; 
HelloWorld* HelloWorld::instance = 0; 
int main(int argc, char* argv[])
{
 HelloWorld::start();
 return 0;
} 

第六種情況:在類外用類內部函式建立執行緒

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream> 
class HelloWorld
{
public:
 void hello(const std::string& str)
 {
        std::cout <<str<< std::endl;
 }
}; 
  
int main(int argc, char* argv[])
{ 
 HelloWorld obj;
 boost::thread thrd( boost::bind(&HelloWorld::hello,&obj,"Hello 
                               world, I''m a thread!" ) ) ;
 thrd.join();
 return 0;
}