最近在作一個消息中間件裏面涉及到多線程編程,因爲跨平臺的緣由我採用了boost線程庫。在建立線程時遇到了幾種線程建立方式現總結以下: ios
首先看看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;
}
第二種方式:複雜類型對象做爲參數來建立線程:
#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;
}
第三種方式:在類內部建立線程;
(1)類內部靜態方法啓動線程
#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方法。
(2)若是要求start()和hello()方法不能是靜態方法則採用下面的方法建立線程:
#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::thread thrd( f );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}
(3)在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;
}
若是線程須要綁定的函數有參數則須要使用boost::bind。好比想使用 boost::thread建立一個線程來執行函數:void f(int i),若是這樣寫:boost::thread thrd(f)是不對的,由於thread構造函數聲明接受的是一個沒有參數且返回類型爲void的型別,並且不提供參數i的值f也沒法運行,這時就能夠寫:boost::thread thrd(boost::bind(f,1))。涉及到有參函數的綁定問題基本上都是boost::thread、boost::function、boost::bind結合起來使用多線程