我有一个boost :: asio threadpool,它有asio::io_service
和asio::work
:
boost::asio::io_service m_service;
boost::asio::io_service::work m_work;
在线程池析构函数中,我调用m_service.stop()
然后加入所有线程。但是在进程关闭期间(在线程池析构函数中)间歇性地运行此崩溃:
#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
asio文档建议明确销毁所有处理程序的asio::work
对象以正常完成。问题:
io_service::stop()
?io_service::stop()
与销毁io_service::work
std::unique_ptr<boost::asio::io_service::work> m_work;
:~threadpool()
{
m_work.reset(); // Allow io_service::run to exit
// join all threads
}
答案 0 :(得分:0)
在明确破坏asio :: work对象后是否需要调用io_service :: stop()?
没有。如果没有更多的工作,服务将完成(轮询/运行将返回)。
io_service :: stop()和破坏io_service :: work
之间有什么区别
工作只是释放锁,允许服务完成。
io_service::stop()
强制停止事件循环,即使仍有待处理的工作。
这个threadpool析构函数是否正确,是否可以帮助解决上述崩溃问题。
是的,析构函数应该加入所有线程。您的代码没有显示,因此我建议您验证是否这样做。
我注意到threadpool
正在从退出处理程序中销毁。这对我来说意味着线程池可能具有静态存储持续时间。如果它还取决于其他任何东西(例如,可能是静态io_service
实例?),您可能会遇到SIOF:静态初始化订单Fiasco。