2. 同步Timer
本章介绍asio如何在定时器上进行阻塞等待(blocking wait).实现,我们包含必要的头文件.
所有的asio类可以简单的通过include "asio.hpp"来调用.
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
一个deadline_timer只有两种状态:到时,未到时.
如果boost::asio::deadline_timer::wait()在到时的timer对象上调用,会立即return.
- t.wait();
- std::cout << "Hello, world! ";
- return 0;
- }
完整的代码:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.wait();
- std::cout << "Hello, world! ";
- return 0;
- }
3. 异步Timer
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& /*e*/)
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.async_wait(print);
asio库只会调用那个正在运行的asio::io_service::run()的回调函数.
如果asio::io_service::run()不被调用,那么回调永远不会发生.
asio::io_service::run()会持续工作到点,这里就是timer到时,回调完成.
别忘了在调用 asio::io_service::run()之前设置好io_service的任务.比如,这里,如果我们忘记先调用asio::deadline_timer::async_wait()则asio::io_service::run()会在瞬间return.
- io.run();
- return 0;
- }
完整的代码:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& /*e*/)
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.async_wait(print);
- io.run();
- return 0;
- }
6. 多线程回调同步
本节演示了使用boost::asio::strand在多线程程序中进行回调同步(synchronise).先前的几节阐明了如何在单线程程序中用boost::asio::io_service::run()进行同步.如您所见,asio库确保 仅当当前线程调用boost::asio::io_service::run()时产生回调.显然,仅在一个线程中调用 boost::asio::io_service::run() 来确保回调是适用于并发编程的.
一个基于asio的程序最好是从单线程入手,但是单线程有如下的限制,这一点在服务器上尤其明显:
- 当回调耗时较长时,反应迟钝.
- 在多核的系统上无能为力
- #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:
boost::asio::strand 可以分配的回调函数.它保证无论有多少线程调用了boost::asio::io_service::run(),下一个回调函数仅在前一个回调函数完成后开始,当然回调函数仍然可以和那些不使用boost::asio::strand分配,或是使用另一个boost::asio::strand分配的回调函数一起并发执行.
- 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_ << " ";
- }
多线程程序中,回调函数在访问共享资源前需要同步.这里共享资源是std::cout 和count_变量.
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++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_ << " ";
- ++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_;
- };
正如单线程中那样,并发的boost::asio::io_service::run()会一直运行直到完成任务.后台的线程将在所有异步线程完成后终结.
- 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;
- }
完整的代码:
- #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_ << " ";
- }
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++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_ << " ";
- ++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;
- }
7. TCP客户端:对准时间
完整的代码:
8. TCP同步时间服务器
完整的代码: