//boost :boost_1_44 //http://www.boost.org/ //IDE :Microsoft Visual Studio 2008 // Version 9.0.30729.1 SP // Microsoft .NET Framework // Version 3.5 SP1 // Installed Edition: Enterprise // Microsoft Visual Basic 2008 // Microsoft Visual C++ 2008 // Microsoft Visual Studio 2008 Team Explorer //OS : //OS 名称: Microsoft Windows XP Professional //OS 版本: 5.1.2600 Service Pack 3 Build 2600 //OS 制造商: Microsoft Corporation //OS 配置: 成员工作站 //OS 构件类型: Multiprocessor Free //系统制造商: Dell Inc. //系统型号: Precision WorkStation 690 //系统类型: X86-based PC //处理器: 安装了 2 个处理器。 //date: 2012-12-19-->21 //CONTENT //Basic Skills //内容引用URL :http://think-async.com/Asio/boost_asio_1_5_3/doc/html/boost_asio/tutorial.html //Using a timer synchronously #include "iostream" #include "boost/asio.hpp" #include "boost/date_time/posix_time/posix_time.hpp" void class1_Timer() { //1. all programs that use asio need to have // at least one io_service object. //2. this class provides access to I/O functionality //3. declare an object of this type first thing in the main function boost::asio::io_service io; //4. declare an object of type boost::asio::deadline_timer //5. the core asio classes that provide I/O functionality // (or as in this case timer functionality)always take a // reference to an io_service as their first constructor argument //6. the second argument to the constructor set the timer to expire // 5 seconds from now. boost::asio::deadline_timer t(io,boost::posix_time::seconds(5)); //7. the call to deadline_timer::wait() will // not return until the timer has expired. // 5 seconds after it was created. SYSTEMTIME sysTimer; GetSystemTime(&sysTimer); std::cout<<sysTimer.wSecond<<std::endl; t.wait();//此时开始计时 GetSystemTime(&sysTimer); std::cout<<sysTimer.wSecond<<std::endl; //差值是5.. } //----------------------------------------------------------------------------> //Using a timer asynchronously //the same head files with class1_Timer() void class2_Timer() { //1. using asio's asynchronous functionality means // having a callback function that will be called // when an asynchronous operation completes boost::asio::io_service io; boost::asio::deadline_timer t(io,boost::posix_time::seconds(5)); //Main Thread,Thread ID 0x000007a0 //设置回调. extern void print(const boost::system::error_code &); t.async_wait(print);//此时开始异步计时. io.run();//会等待计时器过期 //1. io.run()后,这个函数一直运行,此例中io.run()的工作是异步等待定时器t过期. // 当定时器t过期后,io.run()调用设置的回调 //2. 如果不设置回调,io_service::run()会立即返回. std::cout <<"Hello,word--Class2_async_Timer!\n"; } void print(const boost::system::error_code &) { //Main Thread,Thread ID 0x000007a0 std::cout <<"Hello,word!\n"; } //----------------------------------------------------------------------------> //Binding arguments to a handler #include "boost/bind.hpp" void class3_Timer() { boost::asio::io_service io; int count = 0; boost::asio::deadline_timer t(io,boost::posix_time::seconds(1)); extern void print(const boost::system::error_code&/*e*/, boost::asio::deadline_timer *t,int*count); t.async_wait(boost::bind(print,boost::asio::placeholders::error,&t,&count));//定时器t开始计时 io.run();//异步等待t过期,并调用回调print, //print重复异步等待计时器, std::cout<<"Final count is" << count <<"\n"; return ; } void print(const boost::system::error_code&/*e*/, boost::asio::deadline_timer *t, int*count) { if(*count <5) { std::cout << *count <<"\n"; ++(*count); // move the expiry time for the timer along by one second from the previous expiry time //此处的expires_at是根据第一个async_wait开始计算 // 即:在下面的async_wait前断点停止此线程(main thread),计时器线程仍在计时 // main thread停止足够时间,再调用async_wait会立即返回 // async_wait要等待的时间都已经过期. t->expires_at(t->expires_at() + boost::posix_time::seconds(5)); t->async_wait(boost::bind(print,boost::asio::placeholders::error,t,count)); //注意此处不是递归调用.. //只是在函数中将print设置给io作为回调函数,并传递不同的参数... //t->async_wait检测的时间过期即返回.退出print,io取调用新的回调(仍是print) } } //----------------------------------------------------------------------------> //using a member function as a handler class printer{ public: printer(boost::asio::io_service &io) :timer_(io,boost::posix_time::seconds(5)) ,count_(0) { //所有的noe-static成员函数都有this参数,需要提供给boost::bind timer_.async_wait(boost::bind(&printer::print,this)); } ~printer(){ std::cout << "final count is " <<count_ <<"\n"; } void print() { if(count_ < 5) { std::cout << count_ <<"\n"; ++count_; timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(5)); timer_.async_wait(boost::bind(&printer::print,this)); } } private: boost::asio::deadline_timer timer_; int count_; }; void class4_Timer(){ boost::asio::io_service io; printer prt(io); io.run(); } //----------------------------------------------------------------------------> //demonstrates the use of the boost::asio::strand class //to synchronise callback handlers in a multithreaded program //前4个例子都是通过同一个线程调用io.run(),没有handler的同步问题 //回调是被调用io.run()的线程调用. //单线程缺陷 // 长任务执行期间无法获得回应. // 多处理设备无法提升性能 //多线程:当handler处理非线程安全的资源时,需要同步 #include "boost/thread/thread.hpp" class printerMul{ public: printerMul(boost::asio::io_service &io) :strand_(io), timer1_(io,boost::posix_time::seconds(5)), timer2_(io,boost::posix_time::seconds(5)), count_(0){ //被同一个boost::asio::strand warp的handler 将无法并行运行.. //一个handler将在另handler开始前完成. //没有被boost::asio::strand dispatch或者被不同的boost::asio::strand dispatch的handler将仍会并行执行 timer1_.async_wait(strand_.wrap(boost::bind(&printerMul::print1,this))); timer2_.async_wait(strand_.wrap(boost::bind(&printerMul::print2,this))); } ~printerMul(){ std::cout << "Fianl count is " <<count_ <<"\n"; } void print1() { if(count_ <10) { std::cout<<GetCurrentThreadId() <<" "; std::cout<<"Timer1:"<<count_<<"\n"; ++count_; timer1_.expires_at(timer1_.expires_at() \ + boost::posix_time::seconds(2)); timer1_.async_wait(strand_.wrap(boost::bind(&printerMul::print1,this))); } } void print2() { if(count_ < 10) { std::cout<<GetCurrentThreadId() <<" "; std::cout <<"Timer2:"<<count_<<"\n"; ++count_; timer2_.expires_at(timer2_.expires_at()\ +boost::posix_time::seconds(4)); timer2_.async_wait(strand_.wrap(boost::bind(&printerMul::print2,this))); } } private: boost::asio::strand strand_; boost::asio::deadline_timer timer1_; boost::asio::deadline_timer timer2_; int count_; }; void class5_Timer(){ boost::asio::io_service io; printerMul prt(io); //io.run()被两个线程调用.. //1.被boost::thread t 线程调用 boost::thread t(boost::bind(&boost::asio::io_service::run,&io)); //2.被主线程调用 io.run();//(疑问:貌似执行过主线程的io.run()才激活boost::thread t的io.run()?) //print1 与print2两个函数不会同时执行,也不固定谁在哪个线程执行.. t.join(); //Main Thread ID:0x00000bd0 (3024 //共3个线程 //3024 main //1560 boost::thread created //3rd 应该是计时线程.. //cout<< //1560 Timer2:0 //3024 Timer1:1 //1560 Timer1:2 //3024 Timer1:3 //1560 Timer2:4 //3024 Timer1:5 //1560 Timer1:6 //3024 Timer2:7 //3024 Timer1:8 //3024 Timer1:9 //Fianl count is 10 } int main() { int a = 1; while(0 != a) { std::cout << "input your choice"<<std::endl; std::cin >> a; switch(a) { case 1:class1_Timer(); break; case 2:class2_Timer(); break; case 3:class3_Timer(); break; case 4:class4_Timer(); break; case 5:class5_Timer(); break; case 6: { extern void class6(); class6(); } break; default:break; } } return 0; }