io_service 在调用 executor_work_guard::reset 后没有停止

问题描述 投票:0回答:1

我对 boost asio 相当陌生,并试图在调用 work_gaurd 重置函数后干净退出。下面是我试图让它工作的代码。但是我无法打印“IO Context Stopped”

我这里有什么遗漏的吗?

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;                                                                                                                                                                                                                                                
}
c++ boost boost-asio shared-ptr
1个回答
0
投票

代码很好(尽管这里的工作防护是完全多余的):

住在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

奖金

请注意,结果是没有防护装置时相同。更简单的是使用

thread_pool
,这意味着工作警卫:

住在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;
}
© www.soinside.com 2019 - 2024. All rights reserved.