boost::asio::io_service使用时的注意事项

◆boost::asio::io_service使用时的注意事项:

①请让boost::asio::io_service和boost::asio::io_service::work搭配使用。

②想让event按照进入(strand)时的顺序被执行,需要boost::asio::io_service要和boost::asio::io_service::strand搭配使用。

③一般情况下,io_service的成员函数的使用顺序:

boost::asio::io_service构造,
boost::asio::io_service::run(),
boost::asio::io_service::stop(),
boost::asio::io_service::reset(),
boost::asio::io_service::run(),
......
boost::asio::io_service析构,
④不论有没有使用io_service::work,run()都会执行完io_service里面的event,(若没有用work,run就会退出)。
⑤一个新创建的io_service不需要执行reset()函数。
⑥在调用stop()后,在调用run()之前,请先调用reset()函数。
⑦函数stop()和reset()并不能清除掉io_service里面尚未执行的event。
我个人认为,也只有析构掉io_service,才能清空它里面的那些尚未执行的event了。(可以用智能指针)。

⑧函数stop(),stopped(),reset(),很简单,请单步调试,以明白它在函数里做了什么。

⑨boost的.hpp文件里面(一般情况下)有各个函数的使用说明,你可以随时查看。

◆下面是boost::asio::io_service的stop()和reset()函数的注释的翻译:

void boost::asio::io_service::stop();
BOOST_ASIO_DECL void stop();
/// Stop the io_service object's event processing loop.
/// 停止io_service对象的事件处理循环。
/**
 * This function does not block, but instead simply signals the io_service to
 * stop. All invocations of its run() or run_one() member functions should
 * return as soon as possible. Subsequent calls to run(), run_one(), poll()
 * or poll_one() will return immediately until reset() is called.
 */
 /**
 这个函数不阻塞,而是仅仅表示io_service停止了。
 它的run()或run_one()成员函数的调用应当尽快返回。
 对run()、run_one()、poll()、poll_one()的随后的调用将会立即返回直到reset()函数被调用了。
 */
void boost::asio::io_service::reset();
BOOST_ASIO_DECL void reset();
/// Reset the io_service in preparation for a subsequent run() invocation.
/// 重置io_service对象,为随后的run()调用做准备。
/**
 * This function must be called prior to any second or later set of
 * invocations of the run(), run_one(), poll() or poll_one() functions when a
 * previous invocation of these functions returned due to the io_service
 * being stopped or running out of work. After a call to reset(), the
 * io_service object's stopped() function will return @c false.
 *
 * This function must not be called while there are any unfinished calls to
 * the run(), run_one(), poll() or poll_one() functions.
 */
 /**
 io_service被停止,或者执行完handler而缺乏工作时,run()、run_one()、poll()、poll_one()函数的调用会被返回。
 这些函数在被调用之前,必须先调用reset函数。
 在reset函数被调用后,io_service对象的stopped函数将会返回false。
 当run()、run_one()、poll()、poll_one()函数的任何的调用未结束时,这个函数一定不能被调用。
 */

◆对stop()和reset()函数的一点说明(是我单步调试时看到的):

在Windows下,boost::asio::io_service类里面有一个数据成员为"stopped_"(Flag to indicate whether the event loop has been stopped.)。它是一个标志,它标志着事件循环是不是被stopped了。而boost::asio::io_service::reset()函数仅仅是赋值"stopped_=0"。boost::asio::io_service::stopped()函数仅仅是判断"0!=stopped_"的真假。你单步调试一下,就什么都知道了。

猜你喜欢

转载自blog.csdn.net/i7thTool/article/details/82495080