2

deadline_timer::async_wait当我调用我的方法时,偶尔会抛出一个段错误SendMessageAgain。它可以以两种方式之一发生;我在下面包含了回溯。它似乎是随机的,这让我觉得某种程度上涉及到一个竞争条件。我有一个带有io_service对象的类ioService和多个线程,每个线程都有定时器挂钩ioServiceioService打电话之前我可能需要锁定async_wait吗?我认为它处理了那个。

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

我使用的代码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=..., 处理程序=...)
    在 /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=...) 在/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=...)
    在 /usr/include/boost/asio/basic_deadline_timer.hpp:435
#3 0x080555a2 在 Node::SendMessageAgain (this=0xbffeffdc, seqNum=8)
    在 node.cpp:147
#0 __pthread_mutex_lock (mutex=0x2f200c4) 在 pthread_mutex_lock.c:50
#1 0x08056c79 in boost::asio::detail::posix_mutex::lock (this=0x2f200c4)
    在 /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=...)
    在 /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=...,
    时间=...,计时器=...,操作=0x807fca0)
    在 /usr/include/boost/asio/detail/impl/epoll_reactor.hpp:43
#4 0x0806063a 在 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 , 暗示=...,
    处理程序=...)
    在 /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> > > > (这=0xb6b005e8,
    实现=...,处理程序=...)
    在 /usr/include/boost/asio/deadline_timer_service.hpp:135
#6 0x0805bcd1 in boost::asio::basic_deadline_timer<boost::posix_time::ptime, bo---键入 <return> 继续,或 q <return> 退出---
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=...)
    在 /usr/include/boost/asio/basic_deadline_timer.hpp:435
#7 0x080555a7 在 Node::SendMessageAgain (this=0xbffeffdc, seqNum=9)
    在 node.cpp:148
4

2 回答 2

3

好吧,已经修好了。正如 Sam 所建议的,这是一个对象生命周期问题,尽管Node. 我在拥有的对象中有一个计时器Node。我的代码中有一个竞争条件,我在临界区之外重置计时器,并且它的所有者在临界区结束和计时器重置之间被破坏。我只是扩展了关键部分并修复了它。

我不确定为什么段错误会在这么远的地方表现出来async_wait,因为回调(和关联的this指针)属于Node,它仍然存在。

于 2013-04-16T04:18:57.017 回答
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>

如果它是由于您的节点被销毁而仍在为其安排回调而导致的,这可以解决问题。

于 2013-04-16T00:17:39.290 回答