国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

boost::thread用法

2019-11-08 03:23:55
字體:
來源:轉載
供稿:網友

轉自:http://www.cppblog.com/janvy/archive/2010/03/25/110498.aspx

最近在做一個消息中間件里面涉及到多線程編程,由于跨平臺的原因我采用了boost線程庫。在創建線程時遇到了幾種線程創建方式現總結如下: 首先看看boost::thread的構造函數吧,boost::thread有兩個構造函數: (1)thread():構造一個表示當前執行線程的線程對象; (2)explicit thread(const boost::function0& threadfunc): boost::function0可以簡單看為:一個無返回(返回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結合起來使用


上一篇:spring----filed值注入

下一篇:spring AOP

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武威市| 青龙| 柘荣县| 宜宾县| 保山市| 台山市| 青龙| 仙游县| 东安县| 日照市| 兴义市| 抚州市| 丹东市| 喀什市| 自治县| 桃园市| 万盛区| 乌兰察布市| 韶山市| 贵南县| 和龙市| 都江堰市| 汤原县| 民权县| 麻栗坡县| 张家川| 鲜城| 荣成市| 柞水县| 易门县| 项城市| 桓仁| 仁布县| 梧州市| 旬阳县| 南部县| 颍上县| 灵山县| 鄯善县| 林芝县| 宜都市|