如何处理:如果boost::asio::post无限重复,什么时候触发boost::asio::thread_pool析构函数?

How to deal: if boost::asio::post is endlessly repeated, when boost::asio::thread_pool destructor is triggered?

我有 boost::asio::thread_pool m_pool 的 class 包装器。在包装器的析构函数中,我加入了所有线程:

ThreadPool::~ThreadPool()
{
    m_pool.join();
    cout << "All threads in Thread pool were completed";
}

我还有队列方法来向线程池添加新任务:

void ThreadPool::queue(std::function<void()> task, std::string label)
{
    boost::asio::post(m_pool, task);
    cout << "In Thread pool was enqueued task: " << label;
}

在 boost documentation for thread_pool 析构函数中说:

Automatically stops and joins the pool, if not explicitly done beforehand.

如何处理boost::asio::post无限重复,触发boost::asio::thread_pool析构函数的情况? 我会得到无穷无尽的东西吗thread_pool?

1+1 == 2:只需删除 join()。正如您所指出的,这有无限期阻塞的风险。你不会 want/need 那,为什么要问它?

或者,您可以手动停止并加入池。我建议删除析构函数。