摘要: 1.定时器的使用,sleep是等待线程,asio封装了操作系统的异步系统调用select,epoll.io_servie 实现了一个任务队列,这里的任务就是void(void)的函数。Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,run是执行队列中的任务,直到全部执行完毕,并且run可以被N个线程调用。Io_service是完全线程安全的队列。#include #include #include #include #include using namespace std;using namespace boost::asio;using namespace 阅读全文
posted @ 2013-08-13 23:37 l851654152 阅读(3604) 评论(0) 推荐(0) 编辑
摘要: 1.boost里的thread创建之后会立即启动。代码示例:#include #include #include #include #include using namespace std;using namespace boost;mutex io_mu;void printing(int& x,const string str){ for (int i = 0;i #include #include #include #include using namespace std;using namespace boost;mutex io_mu;void printing(int&am 阅读全文
posted @ 2013-08-13 22:54 l851654152 阅读(2212) 评论(0) 推荐(0) 编辑
摘要: 七夕节快乐!人应该有梦想! 阅读全文
posted @ 2013-08-13 21:55 l851654152 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 1.boost里的互斥量类型由mutex表示。代码示例:#include #include #include #include #include using namespace std;using namespace boost;int main(){ mutex mu; try { this_thread::sleep(posix_time::seconds(2)); mu.lock();//锁定cout对象 cout #include #include #include #include using namespace std;using namespace boost;templa... 阅读全文
posted @ 2013-08-13 19:12 l851654152 阅读(16914) 评论(0) 推荐(0) 编辑
摘要: boost里的bind,function,signal三个组件都是对用函数做参数(其他算法也用函数做参数),对函数的某一项进行操作。bind主要是对函数参数的作用。function主要是对函数地址的封装。signal主要是异步回调。用函数做参数时1.普通函数需要传递函数地址。2.函数对象需要传递一个对象3.成员函数需要传递对象,指明所调用的成员函数。如果只有对象则符合规则2. 阅读全文
posted @ 2013-08-13 18:48 l851654152 阅读(576) 评论(0) 推荐(0) 编辑
摘要: boost里的signal是一个模板类,不区分信号种类,产生信号统一用()调用操作符。1.回调普通函数代码示例:#include #include #include #include using namespace std;using namespace boost::signals2;void slots1(){ cout sig; sig.connect(&slots1); sig.connect(&slots2); sig(); return 0;}2.使用组号和注册位置代码示例,可以使用有名对象,传递的是函数对象。#include #include #include # 阅读全文
posted @ 2013-08-13 18:41 l851654152 阅读(401) 评论(0) 推荐(0) 编辑
摘要: boost中function是对函数指针和函数对象的进行封装的模板类。定义示例:function func生成一个空的对象,表示函数参数个数为零,返回类型为int。#include #include #include #include using namespace std;using namespace boost;int f(int a,int b){ return a + b;}int main(){ function fun;//定义一个空的function对象 fun = f;//调用赋值操作符 if (fun) { cout << fun(10,20) << 阅读全文
posted @ 2013-08-13 14:57 l851654152 阅读(214) 评论(0) 推荐(0) 编辑
摘要: bind1st bind2nd在stl里面有具体的实现,只是只能绑定两个参数。boost里面的bind使用bind(Fun f,A1 a1,A2,a2...)产生一个对象,这个对象可以有占位符,可以等到调用函数对象的时候传递参数进去即通过bind b(..);b(args);bind绑定参数是通过存储值的形式,如果对象很大,需要耗费很大的代价,可以用ref,但是bind是延后处理的,需要保证所引用的对象在调用之前不被释放。一.绑定普通函数#include #include #include using namespace std;using namespace boost;int f(int 阅读全文
posted @ 2013-08-13 00:44 l851654152 阅读(362) 评论(0) 推荐(0) 编辑