Home > Software engineering >  How to deal: if boost::asio::post is endlessly repeated, when boost::asio::thread_pool destructor is
How to deal: if boost::asio::post is endlessly repeated, when boost::asio::thread_pool destructor is

Time:05-31

I have a class wrapper for boost::asio::thread_pool m_pool. And in wrapper's destructor i join all the threads:

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

Also I have queue method to add new task to threadpool:

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

In boost documentation for thread_pool destructor is said:

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

How to deal with the situation when I have boost::asio::post endlessly repeated, when boost::asio::thread_pool destructor is triggered? Will I got something like an endless thread_pool?

CodePudding user response:

1 1 == 2: just remove the join(). As you've noted, that risks blocking indefinitely. You don't want/need that, so why ask for it?

Alternatively, you could manually stop and join the pool. I'd suggest removing the destructor.

  • Related