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;
- }
4. 回调函数的参数
这里我们将每秒回调一次,来演示如何回调函数参数的含义- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- 指向timer的指针
- 一个int*来指向计数器
- void print(const asio::error& /*e*/,
- asio::deadline_timer* t, int* count)
- {
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- ...
(原文: By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.)
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
在这个例子中, boost::bind的 asio::placeholders::error参数是为了给回调函数传入一个 error对象.当进行一个异步操作,开始 boost::bind时,你需要使用它来匹配回调函数的参数表.下一节中你会学到回调函数不需要 error参数时可以省略它.
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
- std::cout << "Final count is " << count << " ";
- return 0;
- }
完整的代码:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& /*e*/,
- bsp; asio::deadline_timer* t, int* count)
- {
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
- std::cout << "Final count is " << count << " ";
- return 0;
- }
5. 成员函数作为回调函数
本例的运行结果和上一节类似- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- //构造函数有一个io_service参数,并且在初始化timer_时用到了它.用来计数的count_这里同样作为了成员变量
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
注意,这里没有指定 boost::asio::placeholders::error占位符,因为这个 print成员函数没有接受一个 error对象作为参数.
- timer_.async_wait(boost::bind(&printer::print, this));
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
print函数于上一节的十分类似,但是用成员变量取代了参数.
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
完整的代码:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- 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;
- }