如何做到这一点清楚所有张贴的任务,这在股已经排队? [英] How do it clear all posted tasks which already queued in a strand?
问题描述
如何明确其已在排队的所有张贴任务的 io_service对象::股
?我看到升压文件没有类似的方法。
How do it clear all posted tasks which already queued in a io_service::strand
? I see no similar method from boost document.
推荐答案
我还没有找到需要它,因为它能够正确合理设计异步调用链来解决。一般地,Boost.Asio的API以这样一种方式是经过精心设计,它prevents复杂的应用程序,从在异步流变得复杂。
I have yet to find a need for it, as it can be resolved correctly with properly designing the asynchronous call chains. Generally, the Boost.Asio API is carefully designed in such a way that it prevents complex applications from becoming complicated in the asynchronous flow.
如果您已检查调用链,并且是绝对肯定的是,努力重新设计他们比引入清除链的复杂性更大的当前和未来的风险,那么有一种方法来完成它。然而,它确实有删除在链
,及其相关的 io_service对象
所有未调用处理程序的主要副作用。
If you have examined the call chains, and are absolutely certain that the effort to redesign them is a greater current and future risk than introducing the complication of clearing a strand, then there is a way to accomplish it. However, it does have the major side effect of deleting all uninvoked handlers within the strand
, and its associated io_service
.
在链
被破坏,它的<一个href=\"http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/io_service__strand/_strand.html\"相对=nofollow>析构函数时间表未调用的延期调用处理程序 io_service对象
维护非并发的保证。在 io_service对象
析构函数指出,定于递延调用了未调用处理程序对象被销毁。因此,通过控制寿命的链
和 io_service对象
,可以清除一个链中的处理程序。
When a strand
is destroyed, its destructor schedules uninvoked handlers for deferred invocation on the io_service
maintaining the guarantee of non-concurrency. The io_service
destructor states that uninvoked handler objects that were scheduled for deferred invocation are destroyed. Thus, by controlling the lifetime of a strand
and io_service
, one can clear the handlers in a strand.
下面是一个辅助类,一个过于简单的例子 clearable_strand
。
Here is an overly simplified example with a helper class clearable_strand
.
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/optional.hpp>
#include <boost/utility/in_place_factory.hpp>
class clearable_strand
{
public:
clearable_strand(boost::asio::io_service& main_io_service)
: main_io_service_(main_io_service)
{
clear();
}
public:
template <typename Handler>
void post(const Handler& handler)
{
// Post handler into the local strand.
local_strand_->post(handler);
// Local service now has work, so post its run handler to the
// main service.
main_io_service_.post(boost::bind(
&boost::asio::io_service::run_one, boost::ref(local_io_service_)));
}
void clear()
{
// Destroy previous (if any).
local_strand_ = boost::none;
local_io_service_ = boost::none;
// Reconstruct.
local_io_service_ = boost::in_place();
local_strand_ = boost::in_place(boost::ref(local_io_service_.get()));
}
private:
boost::asio::io_service& main_io_service_;
boost::optional<boost::asio::io_service> local_io_service_;
boost::optional<boost::asio::strand> local_strand_;
};
要尽量减少影响,所以,只有的处理程序被破坏,该类使用内部 io_service对象
链
而不是安装在链
主 io_service对象
。工作时发布到链
,处理程序,然后发布到主 io_service对象
将菊花链方式和维修当地 io_service对象
。
To minimize the effect so that only the strand
's handlers are destroyed, the class uses an internal io_service
rather than attaching the strand
to main io_service
. When work is posted to the strand
, a handler is then posted to the main io_service
that will daisy chain and service the local io_service
.
和其用法:
void print(unsigned int x)
{
std::cout << x << std::endl;
}
int main()
{
boost::asio::io_service io_service;
io_service.post(boost::bind(&print, 1));
clearable_strand strand(io_service);
strand.post(boost::bind(&print, 2));
strand.post(boost::bind(&print, 3));
strand.clear(); // Handler 2 and 3 deleted.
strand.post(boost::bind(&print, 4));
io_service.run();
}
运行程序将输出 1
和 4
。我想强调,这是一个过于简单的例子,在一个非复杂的方式提供线程安全,以匹配的提高:: ASIO ::股
可是一个挑战。
Running the program will output 1
and 4
. I would like to stress that it is an overly simplified example, and providing thread-safety in a non-complicated manner to match that of boost::asio::strand
can be a challenge.
这篇关于如何做到这一点清楚所有张贴的任务,这在股已经排队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!