#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: strand_(io),
timer1_(io, boost::posix_time::seconds(1)),
timer2_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print1()
{
if (count_ < 10)
{
std::cout << "Timer 1: " << count_ << "\n";
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
}
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << "\n";
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
}
private:
boost::asio::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}

线程同步-信号量-strand的用法总结
作者:asio转载自:asio分享学习快乐更新时间:2009-8-2
这里展示不用boost::asio::strand而是利用常规线程同步的手段来完成线程的同步。
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io):
timer1_(io, boost::posix_time::seconds(1)),
timer2_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(boost::bind(&printer::print1, this));
timer2_.async_wait(boost::bind(&printer::print2, this));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print1()
{
mutex_.lock();
if (count_ < 10)
{
std::cout <<"ThreadID:" <<GetCurrentThreadId() <<" Timer 1: " << count_ << "\n";
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(boost::bind(&printer::print1, this));
}
mutex_.unlock();
}
void print2()
{
mutex_.lock();
if (count_ < 10)
{
std::cout <<"ThreadID:" <<GetCurrentThreadId() <<" Timer 2: " << count_ << "\n";
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(boost::bind(&printer::print2, this));
}
mutex_.unlock();
}
private:
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
boost::mutex mutex_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
这样的效果和原boost::asio的例5是差不多的,boost::asio除了支持原生的线程同步方式外还加入了新的asio::strand是有意义的,因为这两种方式还是有区别的。
1. 用mutex的方式阻塞的位置是已经进入printe函数以后,而strand是阻塞在函数调用之前的。
2. 相对来说,当大量的同样回调函数需要同步时,asio::strand的使用更为简单一些。
3. 用mutex的方式明显能够更加灵活,因为不仅可以让线程阻塞在函数的开始,也可以阻塞在中间,结尾。
4. 对于同步的对象来说,asio::strand就是对其支持的回调对象,mutex是对本身线程的一种同步。
基本上,两者是相辅相成的,各有用处,但是实际上,假如从通用性出发,从额外学习知识触发,个人感觉strand似乎是可有可无的,不知道有没有必须使用strand的情况。。。。
到此,asio文档中tutorial中的timer系列例子是结束了。其实这里展示的以asio基本原理为主,甚至都还没有接触到任何与网络相关的东西,但是,这些却是进一步学习的基础。。。。。。
补充:利用strand实现的话:
- 视频教程在线观看
- Visual Basic从入门到精通视频教程
- 李天生SQL2008数据库视频教程
- 李天生二级VF考试视频教程
- 李天生Java软件开发高级视频教程
- 李天生Java软件开发高级教程
- C++从入门到精通视频教程
- 鹏哥C#语言从入门到高级视频教程
- 李天生JavaScript+JQuery视频教程
- C#教程:路径中的空格问题
- Java教程:JAVA时间格式化处理
- Java教程:Java终止函数详解
- Java教程:内存泄漏
- Java教程:IO流之对象流应用
- Java教程:垃圾回收算法4
- Java教程:垃圾回收算法3
- Java教程:垃圾回收算法2
- Java教程:垃圾回收算法 1
- C#教程:16进制转换10进制
- C#教程:账号加密算法
- C#教程:C#运算符重载
- C#教程:获得时间段
- VB.net教程:编写文字加解密程序
- Java教程:处理数据库超时与死锁
- C#教程:借助API实现黑盒自动化测试
- C#教程:分析Dialog控件
- C#教程:调用外部程序
- C#教程:读取数据库返回泛型
- C#教程:字符串截取方法
- C#教程:删除list中的元素
- C#教程:附加数据库
- C#教程:保留小数位
- Delphi教程:数据采集与串口通信测
- JAVA教程:多线程
- VB教程:实例问题一则
- vb教程:实现多线程!
- vb教程:实现JPEG数据压缩
- VB教程:VB中调用VC++开发的DLL
- JAVA教程:UDP数据广播的局域网络会
- C语言教程:C 程序开发经典实例之7
- C语言教程:实现的闹钟程序
- 如何利用用C语言来解决鬼谷算题
- C语言教程:C 程序开发经典实例之一
- C语言教程:C 程序开发经典实例之2
- C语言教程:C 程序开发经典实例之3
- C语言教程:C 程序开发经典实例之4
- C语言教程:C 程序开发经典实例之6
- C语言教程:C 程序开发经典实例之8
- C语言教程:C 程序开发经典实例之9
- C语言教程:C 程序开发经典实例之1
- C语言教程:C语言程序设计技巧
- C语言教程:C 程序开发经典实例之5
- C语言教程:入门基础
- 如何在VB.net中应用HOOK(二)
- 如何在VB.NET中取得计电脑的硬件信
- C语言教程:写好C程序的10条秘籍
- C语言教程:入门基础2
- C语言教程:C语言入门
- VB.NET实例:删除文件时出错
- VB.NET实例:求一个因式分解的程序
- 关于MDI多文档编辑器的问题
- 如何产生不同的随机数?
- VB第三方控件实现XP资源管理器垂直
- VB第三方控件实现XP资源管理器垂直
- Delphi基础教程:开发分隔线组件
- Delphi基础教程:在程序中调用自己
- Delphi基础教程:面向组件的系统开
- Delphi基础教程:创建VCL 构件及开
- Delphi基础教程:编写ASP中的Activ