c++ io_service在调用executor_work_guard::reset后未停止

ahy6op9u  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(133)

我对asio的提升还是个新手,在调用了work_gaurd reset函数后,我试着让它干净的退出。下面是我试图让它工作的代码。然而,我无法获得“IO上下文停止”的打印
有什么我遗漏的吗?

int main() {
  boost::asio::io_context ioContext;                                                                                                                                                                                                                       
  auto work = boost::asio::make_work_guard(ioContext);                                                                                                                                                                                                     
  ioContext.post([]() {                                                                                                                                                                                                                                    
    std::cout << "Task executed on thread: " << std::this_thread::get_id()                                                                                                                                                                                 
              << std::endl;                                                                                                                                                                                                                                
  });                                                                                                                                                                                                                                                      
  std::thread ioThread([&ioContext]() { ioContext.run(); });                                                                                                                                                                                               
  std::this_thread::sleep_for(std::chrono::seconds(2));                                                                                                                                                                                                    
  work.reset();                                                                                                                                                                                                                                            
  //! ioContext.stop();                                                                                                                                                                                                                                    
  ioThread.join();                                                                                                                                                                                                                                         
  std::cout << "IO context stopped." << std::endl;                                                                                                                                                                                                         
  return 0;                                                                                                                                                                                                                                                
}
inkz8wg9

inkz8wg91#

代码很好(尽管这里的work-guard完全是多余的):

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>
using namespace std::chrono_literals;

int main() {
    boost::asio::io_context ioContext;

    auto work = make_work_guard(ioContext);
    post(ioContext, [] { std::cout << "Task executed on thread" << std::endl; });

    std::thread ioThread([&] { ioContext.run(); });

    std::this_thread::sleep_for(2s);
    work.reset();

    ioThread.join();
    std::cout << "IO context stopped." << std::endl;
}

测试与

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && time ./a.out

印刷品,例如

Task executed on thread
IO context stopped.

real    0m2,004s
user    0m0,000s
sys     0m0,004s

奖金

注意,结果是the same without the guard。更简单的是使用thread_pool,这意味着一个工作保护:

Live On Coliru

int main() {
    {
        boost::asio::thread_pool ioContext(1);
        post(ioContext, [] { std::cout << "Task executed on thread" << std::endl; });
        std::this_thread::sleep_for(2s);
        // ioContext.join();
    }

    std::cout << "IO context stopped." << std::endl;
}

相关问题