全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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绑定普通函数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);绑定成员函数int ClassA:ClassFunc(int a, int b) boost:bind(&ClassA:ClassFunc, x, 10, 20);x 可以使类型ClasssA的普通对象、引用或者指针,参数形式也可用使用类似上边的占位符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的普通对象、引用或者指针。在类内部时,x 换成 thismutexboost:mutex mu; 应该使用 try-catch 保证解锁try mu.lock; .; mu.unlock; catch (.) mu.unlock; mutex使用类内部类型定义scopted_lock执行以上的 try-catch,以上代码可简写为:boost:mutex mu; mutex:scoped_lock s_lock(mu);.scope_lock会锁定一个代码块(),一般使用扩住一个代码块,块内第一行使用之;条件变量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 */条件变量必须和互斥一起用,等待时需要传入一个已经上锁的互斥,当条件满足时,直接向下进行;当条件不满足时,等待内部会解锁互斥并将线程挂载等待队列上(PV操作),进入可打断的睡眠状态,当条件变量状态改变(由另一个线程控制)时唤醒,锁定互斥,并向下进行。boost多线程例子thread创建线程(包括普通函数和类成员函数):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 线程组可以创建普通函数作为线程函数的线程: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 线程组也可以创建类成员作为线程函数的线程: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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 27版53高中同步新教材必修上册人教语文第七单元单元群文阅读
- 2026软件开发行业现状深度调研及未来竞争优势研究
- 幼儿保健医模拟试题及答案详解
- 2026中国新能源市场行业市场深度调研及发展趋势和投资前景预测研究报告
- 2026毛线制品行业市场现状设计生产分析投资发展策略评估报告
- 2026中国投资行业市场现状供需分析及投资评估规划发展分析研究报告
- 2026中国印刷机械制造业市场供需竞争投资机会规划分析研究报告
- 2026慢性病医疗服务体系建设规划分析研究书
- 幼儿指南中班测评题及答案
- 2026欧盟医疗器械认证技术要求详解编
- 净水厂调试 运营方案
- 2026年初中历史中国近现代史专题讲座
- 2026年中国建设银行福建省分行消防安全岗社会招聘笔试备考试题及答案解析
- 醉酒后的急救处理与预防方法
- 2026农业4.0智慧农业领航之路行业趋势白皮书
- 2026年三级老年人能力评估师复习复习试题及答案详解(有一套)
- 湖南长沙水业集团有限公司招聘考试真题2025
- 建筑工程项目质量安全管理手册
- 预防安全踩踏事故课件
- 阿尔茨海默病BPSD多学科协作管理方案
- 2025年山东医疗卫生招聘笔试真题
评论
0/150
提交评论