boost多线程使用简例
原文鏈接:http://www.cppblog.com/toMyself/archive/2010/09/22/127347.html
C++ Boost Thread 編程指南
轉(zhuǎn)自cnblog: http://www.cnblogs.com/chengmin/archive/2011/12/29/2306416.html
Boost::Thread使用示例
轉(zhuǎn)自: http://blog.csdn.net/zhuxiaoyang2000/article/details/6588031
羅素實(shí)驗(yàn)室的分析:
轉(zhuǎn)自:? http://www.rosoo.net/a/200912/8082.html
boost鎖的概述:
原文鏈接:http://blog.csdn.net/hbhhww/article/details/7416170
Boost多線程編程
背景
?? 今天互聯(lián)網(wǎng)應(yīng)用服務(wù)程序普遍使用多線程來(lái)提高與多客戶鏈接時(shí)的效率;為了達(dá)到最大的吞吐量,事務(wù)服務(wù)器在單獨(dú)的線程上運(yùn)行服務(wù)程序;
??? GUI應(yīng)用程序?qū)⒛切┵M(fèi)時(shí),復(fù)雜的處理以線程的形式單獨(dú)運(yùn)行,以此來(lái)保證用戶界面能夠及時(shí)響應(yīng)用戶的操作。這樣使用多線程的例子還有很多。
?? 跨平臺(tái)
最近在做一個(gè)消息中間件里面涉及到多線程編程,由于跨平臺(tái)的原因我采用了boost線程庫(kù)。在創(chuàng)建線程時(shí)遇到了幾種線程創(chuàng)建方式現(xiàn)總結(jié)如下:??
? 首先看看boost::thread的構(gòu)造函數(shù)吧,boost::thread有兩個(gè)構(gòu)造函數(shù):?
(1)thread():構(gòu)造一個(gè)表示當(dāng)前執(zhí)行線程的線程對(duì)象;?
(2)explicit thread(const boost::function0<void>& threadfunc):?
???? boost::function0<void>可以簡(jiǎn)單看為:一個(gè)無(wú)返回(返回void),無(wú)參數(shù)的函數(shù)。這里的函數(shù)也可以是類重載operator()構(gòu)成的函數(shù);該構(gòu)造函數(shù)傳入的是函數(shù)對(duì)象而并非是函數(shù)指針,這樣一個(gè)具有一般函數(shù)特性的類也能作為參數(shù)傳入,在下面有例子。?
第一種方式:最簡(jiǎn)單方法?
#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; }
第二種方式:復(fù)雜類型對(duì)象作為參數(shù)來(lái)創(chuàng)建線程:
#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; }
第三種方式:在類內(nèi)部創(chuàng)建線程;?
(1)類內(nèi)部靜態(tài)方法啟動(dòng)線程?
#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()方法不能是靜態(tài)方法則采用下面的方法創(chuàng)建線程:?
#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模式內(nèi)部創(chuàng)建線程:
#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; }
第四種方法:用類內(nèi)部函數(shù)在類外部創(chuàng)建線程;?
#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; } 如果線程需要綁定的函數(shù)有參數(shù)則需要使用boost::bind。比如想使用 boost::thread創(chuàng)建一個(gè)線程來(lái)執(zhí)行函數(shù):void f(int i),如果這樣寫:boost::thread thrd(f)是不對(duì)的,因?yàn)閠hread構(gòu)造函數(shù)聲明接受的是一個(gè)沒有參數(shù)且返回類型為void的型別,而且不提供參數(shù)i的值f也無(wú)法運(yùn)行,這時(shí)就可以寫:boost::thread thrd(boost::bind(f,1))。涉及到有參函數(shù)的綁定問題基本上都是boost::thread、boost::function、boost::bind結(jié)合起來(lái)使用
總結(jié)
以上是生活随笔為你收集整理的boost多线程使用简例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个openMP编程处理图像的示例
- 下一篇: Eigen库对齐问题:declspec(