当我在我的方法SendMessageAgain调用deadline_timer::async_wait ,有时会引发段错误。 它可以通过以下两种方式之一发生: 我在下面包括了回溯。 它似乎是随机的,这使我以某种方式考虑了比赛条件。 我有一个带有io_service对象ioService的类,以及多个线程,每个线程都有与ioService挂钩的ioService 在调用async_wait之前是否可能需要锁定ioService 我以为它可以解决这个问题。

也许它与计时器滴答声中被中断的任何代码有关。 当同时执行其他代码时,设置截止期限计时器的正确方法是什么?

我在SendMessageAgain中使用的代码是

void Node::SendMessageAgain(unsigned long seqNum) {
  // figure out if and what to send (using object fields)
  if (should_send_again) {
    Send(...);
    timer->expires_from_now(INTERVAL);
    timer->async_wait(bind(&Node::SendMessageAgain, this, seqNum));
  }
}
#0  0x08060609 in boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0x14, impl=..., handler=...)
    at /usr/include/boost/asio/detail/deadline_timer_service.hpp:170
#1  0x0805e2e7 in boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0x0, impl=..., 
    handler=...) at /usr/include/boost/asio/deadline_timer_service.hpp:135
#2  0x0805bcbb in boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> > >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0x807fc50, handler=...)
    at /usr/include/boost/asio/basic_deadline_timer.hpp:435
#3  0x080555a2 in Node::SendMessageAgain (this=0xbfffefdc, seqNum=8)
    at node.cpp:147
#0  __pthread_mutex_lock (mutex=0x2f200c4) at pthread_mutex_lock.c:50
#1  0x08056c79 in boost::asio::detail::posix_mutex::lock (this=0x2f200c4)
    at /usr/include/boost/asio/detail/posix_mutex.hpp:52
#2  0x0805a036 in boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>::scoped_lock (this=0xbfffeb04, m=...)
    at /usr/include/boost/asio/detail/scoped_lock.hpp:36
#3  0x08061dd0 in boost::asio::detail::epoll_reactor::schedule_timer<boost::asio::time_traits<boost::posix_time::ptime> > (this=0x2f200ac, queue=..., 
    time=..., timer=..., op=0x807fca0)
    at /usr/include/boost/asio/detail/impl/epoll_reactor.hpp:43
#4  0x0806063a in boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0xb6b005fc, impl=..., 
    handler=...)
    at /usr/include/boost/asio/detail/deadline_timer_service.hpp:170
#5  0x0805e2fd in boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0xb6b005e8, 
    impl=..., handler=...)
    at /usr/include/boost/asio/deadline_timer_service.hpp:135
#6  0x0805bcd1 in boost::asio::basic_deadline_timer<boost::posix_time::ptime, bo---Type <return> to continue, or q <return> to quit---
ost::asio::time_traits<boost::posix_time::ptime>, boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> > >::async_wait<boost::_bi::bind_t<void, boost::_mfi::mf1<void, Node, unsigned long>, boost::_bi::list2<boost::_bi::value<Node*>, boost::_bi::value<unsigned long> > > > (this=0xb6b005a0, handler=...)
    at /usr/include/boost/asio/basic_deadline_timer.hpp:435
#7  0x080555a7 in Node::SendMessageAgain (this=0xbfffefdc, seqNum=9)
    at node.cpp:148

===============>>#1 票数:2 已采纳

好吧,它是固定的。 正如Sam所建议的那样,这是对象生命周期的问题,尽管Node却不是。 我在Node拥有的对象中有一个计时器。 我的代码中有一个竞争条件,我要在关键部分之外重置计时器,并且在关键部分结束和计时器重置之间其所有者被销毁。 我只是扩展了关键部分,并对其进行了修复。

我不确定为什么段错误会在async_wait如此低地表现出来,因为回调(以及与this指针相关联)属于Node ,而Node仍然存在。

===============>>#2 票数:0

坏:

timer->async_wait(bind(&Node::SendMessageAgain, this, seqNum, _1));

好:

timer->async_wait(bind(&Node::SendMessageAgain, shared_from_this(), seqNum, _1));

让节点扩展enable_shared_from_this

class Node : public boost::enable_shared_from_this<Node>

如果这是由于您的Node被销毁而仍在为其安排回调的话,则可以解决此问题。

  ask by Nick translate from so

未解决问题?本站智能推荐: