boost::asio学习 - multithreaded programs
2010-09-27 17:26
471 查看
今天在测试boost::asio Tutorial中的 Timer.5 - Synchronising handlers in multithreaded programs 程序时,发现数据处理始终是在主线程中执行的,为了验证“If you find yourself running into these limitations, an alternative approach is to have a pool of threads calling io_service::run()”遂对timer.cpp进行了一些修改:
1.取消使用boost::asio::strand::wrap()来封装回调函数;
2.在printer::print1()中强制线程sleep(1)来检测多线程的并发处理;
完整代码如下:
timer.cpp
程序运行输出:
0x850e050
0x850e050
Timer 1: 0
0x850e810
Timer 2: 1
0x850e810
Timer 1: 2
0x850e050
Timer 2: 3
0x850e050
Timer 1: 4
0x850e810
Timer 2: 5
0x850e810
Timer 1: 6
0x850e050
Timer 2: 7
0x850e050
Timer 1: 8
0x850e810
Timer 2: 9
0x850e810
0x850e050
从输出结果可以看出:当多个线程运行同一个io_service::run()时,它们将并发执行该io_service上的待处理的handlers!
1.取消使用boost::asio::strand::wrap()来封装回调函数;
2.在printer::print1()中强制线程sleep(1)来检测多线程的并发处理;
完整代码如下:
timer.cpp
#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(boost::bind(&printer::print1, this)); timer2_.async_wait(boost::bind(&printer::print2, this)); } ~printer() { std::cout << "Final count is " << count_ << "\n"; } void print1() { std::cout << boost::this_thread::get_id() << "\n"; if (count_ < 10) { std::cout << "Timer 1: " << count_ << "\n"; ++count_; timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1)); timer1_.async_wait(boost::bind(&printer::print1, this)); } sleep(1); } void print2() { std::cout << boost::this_thread::get_id() << "\n"; if (count_ < 10) { std::cout << "Timer 2: " << count_ << "\n"; ++count_; timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1)); timer2_.async_wait(boost::bind(&printer::print2, this)); } } private: boost::asio::strand strand_; boost::asio::deadline_timer timer1_; boost::asio::deadline_timer timer2_; int count_; }; int main() { std::cout << boost::this_thread::get_id() << "\n"; 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; }
程序运行输出:
0x850e050
0x850e050
Timer 1: 0
0x850e810
Timer 2: 1
0x850e810
Timer 1: 2
0x850e050
Timer 2: 3
0x850e050
Timer 1: 4
0x850e810
Timer 2: 5
0x850e810
Timer 1: 6
0x850e050
Timer 2: 7
0x850e050
Timer 1: 8
0x850e810
Timer 2: 9
0x850e810
0x850e050
从输出结果可以看出:当多个线程运行同一个io_service::run()时,它们将并发执行该io_service上的待处理的handlers!
相关文章推荐
- Boost Asio multi-threaded blocking server/client
- Boost.Asio学习之同步echo服务器实现
- boost学习之asio—chat
- Boost.Asio学习之总结
- boost::asio学习 - 常用方法总结
- boost::asio学习之[六]简单聊天程序
- boost asio 学习1
- boost-asio学习1——定时器(同步、异步)
- boost.asio学习笔记一、linux下boost库的安装
- boost::asio学习 - HTTP Server性能测试报告
- boost asio io_service学习笔记
- 【boost学习】之boost::asio(4)——boost::asio::buffer用法
- boost.asio学习笔记一、linux下boost库的安装
- 『boost』ASIO学习
- Multithreaded singleton - Design Patterns 学习笔记(5)
- xgboost学习样例之multiclass_classification
- boost::asio学习之[三 .2]异步tcp service
- boost asio io_service学习笔记
- boost::asio学习文章
- boost::asio学习 - HTTP Server性能测试报告