C++ boost常用組件.docx_第1頁
C++ boost常用組件.docx_第2頁
C++ boost常用組件.docx_第3頁
C++ boost常用組件.docx_第4頁
全文預覽已結(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論