1

I have a boost::asio threadpool, which has a asio::io_service and asio::work:
boost::asio::io_service m_service;
boost::asio::io_service::work m_work;
In the threadpool destructor, I am calling m_service.stop() and then joining all the threads. However running into this crash intermittently during process shutdown (in the threadpool destructor):



    #0  0x0000000000000000 in ?? ()
    #1  0x00007f4c8e59d160 in boost::asio::detail::task_io_service_operation::destroy (
        this=0x6d579f8)
        at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service_operation.hpp:43
    #2  0x00007f4c8e59d115 in boost::asio::detail::op_queue_access::destroy (o=0x6d579f8)
        at /home/depot/Boost_1_61_0/boost/asio/detail/op_queue.hpp:47
    #3  0x00007f4c8e59d044 in boost::asio::detail::op_queue::~op_queue (this=0x6d57a20)
        at /home/depot/Boost_1_61_0/boost/asio/detail/op_queue.hpp:81
    #4  0x00007f4c8e685d65 in boost::asio::detail::task_io_service::~task_io_service (
        this=0x6d57960)
        at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service.hpp:40
    #5  0x00007f4c8e685dc9 in boost::asio::detail::task_io_service::~task_io_service (
        this=0x6d57960)
        at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service.hpp:40
    #6  0x00007f4c8e5aa1ce in boost::asio::detail::service_registry::destroy (
        service=0x6d57960)
        at /home/depot/Boost_1_61_0/boost/asio/detail/impl/service_registry.ipp:101
    #7  0x00007f4c8e68537c in boost::asio::detail::service_registry::~service_registry (
        this=0x6d55020)
        at /home/depot/Boost_1_61_0/boost/asio/detail/impl/service_registry.ipp:45
    #8  0x00007f4c8e6852b0 in boost::asio::io_service::~io_service (
        this=0x7f4c8eb169c8 )
        at /home/depot/Boost_1_61_0/boost/asio/impl/io_service.ipp:53
    #9  0x00007f4c8e71d295 in crossplat::threadpool::~threadpool (
        this=0x7f4c8eb169b0 )
        at /home/depot/include/threadpool.h:87
    #10 0x00007f4c8aed6fe8 in __run_exit_handlers (status=0,
    ---Type  to continue, or q  to quit---
        listp=0x7f4c8b2605f8 , run_list_atexit=run_list_atexit@entry=true)
        at exit.c:82
    #11 0x00007f4c8aed7035 in __GI_exit (status=) at exit.c:104
    #12 0x00007f4c8aebd837 in __libc_start_main (main=0x1ce0980 ,
        argc=3, argv=0x7fffee2eda98, init=, fini=,
        rtld_fini=, stack_end=0x7fffee2eda88) at ../csu/libc-start.c:325
    #13 0x0000000001cdf159 in _start () at /usr/include/c++/v1/string:2027

The asio documentation suggests to explicitly destroy the asio::work object for all handlers to finish normally. Questions:

  1. Is it required to call io_service::stop() after explicitly destructing the asio::work object?
  2. What is the difference between io_service::stop() and destroying io_service::work
  3. Does this threadpool destructor look right, can it help fix the above crash. I have changed m_work to a pointer std::unique_ptr<boost::asio::io_service::work> m_work;:
    ~threadpool()
    {
    m_work.reset(); // Allow io_service::run to exit
    // join all threads
    }
4

1 回答 1

0

显式破坏 asio::work 对象后是否需要调用 io_service::stop() ?

不会。如果没有更多工作,服务将完成(poll/run 将返回)。

io_service::stop() 和销毁 io_service::work 有什么区别

工作只是释放锁,服务完成。

io_service::stop()强制停止事件循环,即使仍有待处理的工作。

这个线程池析构函数看起来是否正确,它可以帮助修复上述崩溃。

是的,析构函数应该加入所有线程。您的代码没有显示它,所以我建议验证您是否这样做。

主意

我注意到threadpool正在从退出处理程序中销毁。这 - 对我来说 - 意味着线程池可能具有静态存储持续时间。如果它还依赖于其他任何东西(比如,也许是静态io_service实例?),您可能会遇到SIOF:静态初始化订单惨败。

于 2017-04-21T10:56:15.503 回答