日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

boost多线程使用简例

發(fā)布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 boost多线程使用简例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文鏈接: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

羅素實驗室的分析:

轉(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ù)程序普遍使用多線程來提高與多客戶鏈接時的效率;為了達到最大的吞吐量,事務(wù)服務(wù)器在單獨的線程上運行服務(wù)程序;

??? GUI應(yīng)用程序?qū)⒛切┵M時,復(fù)雜的處理以線程的形式單獨運行,以此來保證用戶界面能夠及時響應(yīng)用戶的操作。這樣使用多線程的例子還有很多。

?? 跨平臺

最近在做一個消息中間件里面涉及到多線程編程,由于跨平臺的原因我采用了boost線程庫。在創(chuàng)建線程時遇到了幾種線程創(chuàng)建方式現(xiàn)總結(jié)如下:??
? 首先看看boost::thread的構(gòu)造函數(shù)吧,boost::thread有兩個構(gòu)造函數(shù):?
(1)thread():構(gòu)造一個表示當前執(zhí)行線程的線程對象;?
(2)explicit thread(const boost::function0<void>& threadfunc):?
???? boost::function0<void>可以簡單看為:一個無返回(返回void),無參數(shù)的函數(shù)。這里的函數(shù)也可以是類重載operator()構(gòu)成的函數(shù);該構(gòu)造函數(shù)傳入的是函數(shù)對象而并非是函數(shù)指針,這樣一個具有一般函數(shù)特性的類也能作為參數(shù)傳入,在下面有例子。?
第一種方式:最簡單方法?

#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ù)雜類型對象作為參數(shù)來創(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)方法啟動線程?
#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)建一個線程來執(zhí)行函數(shù):void f(int i),如果這樣寫:boost::thread thrd(f)是不對的,因為thread構(gòu)造函數(shù)聲明接受的是一個沒有參數(shù)且返回類型為void的型別,而且不提供參數(shù)i的值f也無法運行,這時就可以寫:boost::thread thrd(boost::bind(f,1))。涉及到有參函數(shù)的綁定問題基本上都是boost::thread、boost::function、boost::bind結(jié)合起來使用

總結(jié)

以上是生活随笔為你收集整理的boost多线程使用简例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。