#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实现的话:
- SQLite数据库最适合做网站内容管理
- 详解有关线程组的操作示例
- 详解atoi () 函数
- 关于C与C++头文件一览
- 关于C/C++数组名与指针区别详解
- Symbian的异步框架
- 比较IronPython和C#的执行速度
- 我的Visual C++入门之路2
- 我的Visual C++入门之路1
- PHP编写的掷骰器脚本
- PHP编写的乐透机游戏脚本
- PHP编写的米德里比斯游戏脚本全过程
- PH编写的扑克牌器脚本
- PHP编写的随机名称生成器脚本
- PHP编写的场景生成器脚本
- C++ STL 算法另类使用方法
- c++教程之开发中数据结构和算法的分
- C++中禁止异常信息传递到析构函数外
- C++教程之利用非托管并列缓存
- 非对称密码算法制作共享软件的注册
- C++教程之遗传算法详解
- C++中的常规抽象详解
- C/C++程序中内存的的分配详解
- C++教程之快速计算32位数中1的位数
- Java中得抽象数据类型
- 提高编程效率的几点小技巧
- Java面向对象编程实例详解
- Java复杂数据类型用法
- C语言源代码——贪吃蛇游戏
- Visual C++源代码——俄罗斯方块程
- VC编程全过程—“幸运52”猜价格游
- VB.NET和Java得OOP设计
- Delphi编程实现3D图形修饰技术
- 一道JAVA题
- 理解端口,系统服务,系统进程概念
- C语言编程较容易易犯的错误总结
- Boost 库中的实用工具类使用入门(
- Linux/Unix网络编程指南(Socket 编
- Linux/Unix网络编程指南(Socket 编
- Linux/Unix网络编程指南(Socket 编
- Linux/Unix网络编程指南(Socket 编
- 如何编写PHP程序增加一个系统用户
- VB下如何编写CRC校验程序
- Linux/Unix网络编程指南(Socket 编
- ASP与JSP的比较
- Linux/Unix网络编程指南(Socket 编
- 算法合集
- 什么叫做状态机?
- 排序算法实现大全
- C语言常用宏定义技巧
- C语言常用排序全解
- linux内核基本数据类型总结
- 程序员数据结构笔记
- C语言难点分析整理
- C语言高效编程秘籍
- VB与VC混合编程中处理消息的方法
- boost::asio::error的用法浅析
- Boost 库中的实用工具类使用入门(
- Boost 库中的实用工具类使用入门(
- 线程同步-信号量-strand的用法总结
- Access二级视频教程
- 平面设计视频教程
- 编程开发视频教程
- 三维动画视频教程
- 网页设计视频教程
- Flash动画视频教程
- 办公软件视频教程
- 网店技巧视频教程
- 硬件技术视频教程
- 等级考试视频教程