boost asio计时器是否会在“取消”时阻塞?

时间:2018-07-11 06:36:23

标签: c++ boost-asio

我观察到,当boost::asio::steady_timer被调用并且cancel提供的回调已经执行时,async_wait正在阻塞。这是预期的行为吗?它是可配置的吗?为什么首先要阻止它?

调用堆栈:

#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1  0x00007f4d4c703dbd in __GI___pthread_mutex_lock (mutex=0x20000616f558) at ../nptl/pthread_mutex_lock.c:80
#2  0x000070000086ef75 in boost::asio::detail::posix_mutex::lock (this=0x20000616f558) at /source/boost/include/boost/asio/detail/posix_mutex.hpp:52
#3  0x000070000087036e in boost::asio::detail::conditionally_enabled_mutex::scoped_lock::scoped_lock (this=0x40000623e970, m=...) at /source/boost/include/boost/asio/detail/conditionally_enabled_mutex.hpp:55
#4  0x00007000009ebf7f in boost::asio::detail::epoll_reactor::cancel_timer<boost::asio::detail::chrono_time_traits<std::chrono::_V2::steady_clock, boost::asio::wait_traits<std::chrono::_V2::steady_clock> > > (this=0x20000616f520, queue=..., timer=..., 
    max_cancelled=18446744073709551615) at /source/boost/include/boost/asio/detail/impl/epoll_reactor.hpp:62
#5  0x00007000009e8322 in boost::asio::detail::deadline_timer_service<boost::asio::detail::chrono_time_traits<std::chrono::_V2::steady_clock, boost::asio::wait_traits<std::chrono::_V2::steady_clock> > >::cancel (this=0x200006188f60, impl=..., ec=...)
    at /source/boost/include/boost/asio/detail/deadline_timer_service.hpp:144
#6  0x00007000009e5211 in boost::asio::basic_waitable_timer<std::chrono::_V2::steady_clock, boost::asio::wait_traits<std::chrono::_V2::steady_clock> >::cancel (this=0x20009470cfc8)
    at /source/boost/include/boost/asio/basic_waitable_timer.hpp:329

1 个答案:

答案 0 :(得分:0)

看起来问题出在我们正在多进程环境中的事实。所有进程共享相同的内存,并在此共享内存上创建的所有对象(包括互斥锁,线程等)。为了在这种情况下正常运行,系统中使用的互斥锁是使用PTHREAD_PROCESS_SHARED属性创建的。显然,asio互斥锁不是使用这样的属性创建的,所以我想这就是互斥锁卡在意外位置的问题。 io_contextsteady_timer一旦开始仅在一个进程中执行,便开始按预期工作