簡體   English   中英

從io_context或使用多個io_context對象刪除工作

[英]Remove work from a io_context or using multiple io_context objects

目前,我正在嘗試刪除通過postdispatchio_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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM