Timer.2 - Using a timer asynchronously
https://www.boost.org/doc/libs/1_74_0/doc/html/boost_asio/tutorial/tuttimer2.html
Timer.2 - Using a timer asynchronously
本篇教程会演示如何用asio的异步回调函数功能来把上篇教程(Timer.1)中实现的阻塞计时器改成异步实现.
先引入头文件
#include <iostream>
#include <boost/asio.hpp>
要想用asio的异步功能,意味着咱们得写一个回调函数, 等异步操作结束的时候让asio自行调用这个回调函数,这样就是异步方式了.所以在这次的程序里,咱们先定义一个叫做 print 的函数,等会儿作为计时器的回调函数,让它完事时调用这个 print 函数.
void print(const boost::system:error_code& /*e*/)
{
std::cout << "Hello, world!" << std::endl;
}
int main()
{
boost::asio::io_context io;
boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
然后,因为是异步方式嘛,我们把上篇教程中所用的阻塞式函数steady_timer::wait()改成steady_timer::async_wait()函数.然后把准备好的 print函数 给传进去,作为这个异步操作的回调函数.
t.async_wait(&print);
最后,为了让异步操作跑起来,我们必须还要调用io_context类对象的成员函数io_context::run().
io.run();
这是个什么操作?
是这样的,Asio库它保证:异步操作的回调函数只会在io_context::run()的执行线程上被执行.所以,如果不执行io_context::run()的话,那我们的回调函数就永远不会被执行.
(翻译的有亿点点拗口哈, 这是原文)
The asio library provides a guarantee that callback handlers will only be called from threads that are currently calling io_context::run(). Therefore unless the io_context::run() function is called the callback for the asynchronous wait completion will never be invoked.
我(妄)所(加)理(猜)解(测)的是:异步操作由io_context管理,回调函数在io_context::run()内被处理.因此,只有调用了io_context::run(),回调函数才能被处理.
这个io_context::run()函数会在它"手头有活儿"的时候持续执行(活儿干完就return了).在本例中,这个"活儿"就是计时器的倒计时操作和倒计时结束后要执行的回调函数,所以在这两件事(等待倒计时结束和执行回调函数)做完之前,它是不会return的.
有一点一定要记住,就是调用io_context::run()之前一定要给io_context安排点活儿干,这样它才能保持坚挺.比方说,假如我们前面把steady_timer::async_wait()给忘了,那这io_context没什么事干,那io_context::run()一启动就好了.
return 0;
}
再把整体程序回顾一下
#include <iostream>
#include <boost/asio.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!" << std::endl;
}
int main()
{
boost::asio::io_context io;
boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
t.async_wait(&print); // 当异步操作wait结束时,执行回调函数print()
io.run();
return 0;
}
Timer.2 - Using a timer asynchronously
https://www.boost.org/doc/libs/1_74_0/doc/html/boost_asio/tutorial/tuttimer2.html