从boost :: asio :: io_service中删除所有处理程序,而不调用它们 [英] Remove all handlers from a boost::asio::io_service without calling them
问题描述
我想在重复使用之前,从IO_service中删除所有处理程序。这是可能吗?
I want to remove all handlers from an IO_service right before I reuse it. Is this possible?
我正在编写涉及 asio :: io_service
的单元测试。在每个测试用例之间,我想从全局 io_service
中清除处理程序。我认为 io_service :: reset
会那样,但它不。 reset()
只允许恢复 io_service
。所有的处理程序从最后一个测试用例仍然排队。
I'm writing unit tests that involve an asio::io_service
. In between each test case I want to clear the handlers from the global io_service
. I thought that io_service::reset
would to that but it doesn't. reset()
only allows the io_service
to be resumed. All of the handlers from the last test case are still queued up.
我只需要这样做单元测试,所以任何疯狂的黑客都可以工作。
I only need to do this for unit testing so any crazy hack would work.
更多信息:
io_service
来自 deadline_timer
成员变量。 deadline_timer
是我测试的代码的一部分,所以我不能改变它的构造。我通过 deadline_timer
的 get_io_service $ c $保持其
io_service
c>方法。
The io_service
is from a deadline_timer
member variable. The deadline_timer
is part of the code I'm testing so I can't change how it's constructed. I get a hold of its io_service
via the deadline_timer
's get_io_service
method.
推荐答案
好吧,我把我的大脑放在这里几天,并提出了一个可行的解决方案。这是所有黑客的母亲。
Well, I racked my brain on this for a few days and came up with a workable solution. It's the mother of all hacks.
void clear( boost::asio::io_service& service )
{
service.stop();
service.~io_service();
new( &service ) boost::asio::io_service;
}
我不知道这对生产代码有多安全。但到目前为止,它似乎工作(没有segfaults,没有奇怪的行为)。
I'm not sure how safe this would be for productions code. But so far it seems to work (no segfaults, no weird behavior).
这篇关于从boost :: asio :: io_service中删除所有处理程序,而不调用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!