目前,我正在尝试将通过post
或dispatch
排队到io_context
的工作删除。作品由少量队列组排队,对于这些队列组,应立即将其全部删除:
boost::asio::io_context context;
auto work = [] {
// ...
};
boost::asio::post(context, std::move(work));
// ... now I want to remove the work
asio库提供了这种功能吗?
目前,我正在处理的应用程序正在使用线程池,该线程池从多个线程调用
io_context::run()
。我的想法是,我可以创建由线程池调度的多个
io_context
,以便一个io_context
表示可以通过io_context::stop()
删除的一组。所有io_context
都将保存在一个列表中,然后将其合并以处理未决事件。但是,我认为池化或等待许多
io_context
可能会导致性能问题。有其他解决方案吗?
最佳答案
不,没有机制可以从io_context
中删除已发布的作业。另外,您可以修改作业以检查是否运行了“取消标志”(未测试):
// create a cancellation flag
const auto cancel = std::make_shared<std::atomic<bool> >();
auto work = [=] {
// check to see if the flag has been set
// if so, return without performing our task
if(*cancel)
return;
// perform some task
};
// post our job
boost::asio::post(context, std::move(work));
...
// cancel all jobs checking this flag
*cancel = true;