



全文預覽已結(jié)束
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
C+ boost常用組件2012年12月24日14:55nocopyable: class DoNotCopy: boost:noncopyable .;singleton: class BlaBla .;typedef boost:singleton_default BLABLA;assert默認情況boost:BOOST_ASSERT(expr) 等同于 C 的 assert(expr)在#include 之前加上#define BOOST_DISABLE_ASSERTS,boost:BOOST_ASSERT將失效foreachvector v;string str = abcdefg; / or char *. / assignboost:BOOST_FOREACH(int x, v) printf(%d, , x); boost:BOOST_REVERSE_FOREACH(char c, str) printf(%c-, c); bind綁定普通函數(shù)int NormalFunc(int a, int b) boost:bind(NormalFunc, 10, 20); / NormalFunc(10, 20);boost:bind(NormalFunc, _1, 20)(x); / NormalFunc(x, 20);boost:bind(Normalfunc, _1, _2)(x, y); / NormalFunc(x, y);boost:bind(Normalfunc, _2, _1)(x, y); / NormalFunc(y, x);boost:bind(Normalfunc, _1, _1)(x, y); / NormalFunc(x, x);boost:bind(NormalFunc, 10, _1)(x); / NormalFunc(x, 20);綁定成員函數(shù)int ClassA:ClassFunc(int a, int b) boost:bind(&ClassA:ClassFunc, x, 10, 20);x 可以使類型ClasssA的普通對象、引用或者指針,參數(shù)形式也可用使用類似上邊的占位符functionfunction func1;function func2; / or function func2;int Func1() int Func2(int a, int b) func1 = Func1;func2 = Func2;threadthreadint NormalFunc(int a, int b) boost:thread(NormalFunc, 10, 20); / 使用臨時變量joinboost:thread t1(NormalFunc, 10, 20);boost:thread t2(NormalFunc, 100, 200);t1.timed_join(posix_time:seconds(10); / 最多join 10秒后返回t2.join();interrupt線程只有到中斷點才能被中斷掉thread_groupboost:thread tg;tg.create_thread(NormalFunc, 10, 20);tg.create_thread(NormalFunc, 101, 200);類成員線程boost:thread t1(boost:bind(ClassA:ClassFunc, x, 10, 20);boost:thread tg;tg.create_thread(boost:bind(ClassA:ClassFunc, x, 10, 20);x 可以使類型ClasssA的普通對象、引用或者指針。在類內(nèi)部時,x 換成 thismutexboost:mutex mu; 應該使用 try-catch 保證解鎖try mu.lock; .; mu.unlock; catch (.) mu.unlock; mutex使用類內(nèi)部類型定義scopted_lock執(zhí)行以上的 try-catch,以上代碼可簡寫為:boost:mutex mu; mutex:scoped_lock s_lock(mu);.scope_lock會鎖定一個代碼塊(),一般使用擴住一個代碼塊,塊內(nèi)第一行使用之;條件變量mutex mu;boost:condition_variable_any cond;boost:mutex:scope_lock sLock(mu);cond.wait(mu);/* scope unlock */* another thread */boost:mutex:scope_lock sLock(mu);cond.notify_one();cond.notify_all();/* scope unlock */條件變量必須和互斥一起用,等待時需要傳入一個已經(jīng)上鎖的互斥,當條件滿足時,直接向下進行;當條件不滿足時,等待內(nèi)部會解鎖互斥并將線程掛載等待隊列上(PV操作),進入可打斷的睡眠狀態(tài),當條件變量狀態(tài)改變(由另一個線程控制)時喚醒,鎖定互斥,并向下進行。boost多線程例子thread創(chuàng)建線程(包括普通函數(shù)和類成員函數(shù)):int IntFun(int a, int b) printf(IntFun() startn); sleep(3); printf(InfFun() endn);void VoidFun(void) printf(VoidFun() startn); sleep(2); printf(VoidFun() endn);int main() printf(Main() startn); thread(IntFun, 1, 2); / 臨時變量 thread t1(IntFun, 1, 2); thread t2(bind(IntFun, 1, 2); thread t3(bind(ClassA:ClassFunc, x, 1, 2); / 綁定類成員 threads.create_thread(boost:bind(IntFun, 1, 2); threads.create_thread(boost:bind(VoidFun); printf(Main() stopn); return 0;boost 線程組可以創(chuàng)建普通函數(shù)作為線程函數(shù)的線程:int IntFun(int a, int b) printf(IntFun() startn); sleep(3); printf(InfFun() endn);void VoidFun(void) printf(VoidFun() startn); sleep(2); printf(VoidFun() endn);int main() printf(Main() startn); boost:thread_group threads; threads.create_thread(boost:bind(IntFun, 1, 2); threads.create_thread(boost:bind(VoidFun); printf(Main() stopn); return 0;boost 線程組也可以創(chuàng)建類成員作為線程函數(shù)的線程:class A public: int IntFunA(int a, int b) printf(IntFunA() startn); sleep(3); printf(InfFunA() endn); return 0; void VoidFunA(void) printf(VoidFunA() startn); sleep(2); printf(VoidFunA() endn); ;int main() printf(Main() startn); A a; boost:thread_g
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 幼兒園區(qū)角設計教研實施框架
- 偏癱并發(fā)癥肩痛
- 譫妄的評估和護理
- 嗜酸細胞性肺炎臨床診療解析
- 心理健康為人之道
- 幼師職業(yè)護眼健康管理
- 火炬?zhèn)鬟f健康教案大班
- 2025年殯葬服務項目規(guī)劃申請報告
- 【臺州】2025年浙江臺州市交通工程試驗檢測中心招聘22人筆試歷年典型考題及考點剖析附帶答案詳解
- 整活班會課件
- 法院法警考試試題及答案
- 應急工器具培訓課件
- 中國食用油市場調(diào)研及發(fā)展策略研究報告2025-2028版
- 2024年財政部會計法律法規(guī)答題活動題目及答案一
- 質(zhì)量管理部半年總結(jié)PPT
- 2022年德宏傣族景頗族自治州工會系統(tǒng)招聘考試題庫及答案解析
- 雪山上的達娃讀后感范文5篇
- 安全生產(chǎn)基本情況調(diào)研表
- 西方音樂史名詞解釋復習
- 華為自評審核表 Quality System Audit
- 經(jīng)緯度數(shù)轉(zhuǎn)換工具
評論
0/150
提交評論