C++ boost常用組件.docx_第1頁(yè)
C++ boost常用組件.docx_第2頁(yè)
C++ boost常用組件.docx_第3頁(yè)
C++ boost常用組件.docx_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

C+ boost常用組件2012年12月24日14:55nocopyable: class DoNotCopy: boost:noncopyable .;singleton: class BlaBla .;typedef boost:singleton_default BLABLA;assert默認(rèn)情況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的普通對(duì)象、引用或者指針,參數(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); / 使用臨時(shí)變量joinboost:thread t1(NormalFunc, 10, 20);boost:thread t2(NormalFunc, 100, 200);t1.timed_join(posix_time:seconds(10); / 最多join 10秒后返回t2.join();interrupt線程只有到中斷點(diǎn)才能被中斷掉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的普通對(duì)象、引用或者指針。在類內(nèi)部時(shí),x 換成 thismutexboost:mutex mu; 應(yīng)該使用 try-catch 保證解鎖try mu.lock; .; mu.unlock; catch (.) mu.unlock; mutex使用類內(nèi)部類型定義scopted_lock執(zhí)行以上的 try-catch,以上代碼可簡(jiǎn)寫(xiě)為:boost:mutex mu; mutex:scoped_lock s_lock(mu);.scope_lock會(huì)鎖定一個(gè)代碼塊(),一般使用擴(kuò)住一個(gè)代碼塊,塊內(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 */條件變量必須和互斥一起用,等待時(shí)需要傳入一個(gè)已經(jīng)上鎖的互斥,當(dāng)條件滿足時(shí),直接向下進(jìn)行;當(dāng)條件不滿足時(shí),等待內(nèi)部會(huì)解鎖互斥并將線程掛載等待隊(duì)列上(PV操作),進(jìn)入可打斷的睡眠狀態(tài),當(dāng)條件變量狀態(tài)改變(由另一個(gè)線程控制)時(shí)喚醒,鎖定互斥,并向下進(jìn)行。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); / 臨時(shí)變量 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論