增强异步等待使用期间的竞争条件

时间:2017-06-13 16:16:51

标签: c++ boost async-await

根据提升文档:  对于每次调用async_wait(),提供的处理程序将被调用一次。处理程序将在以下情况下调用:   - 计时器已过期。   - 计时器被取消,在这种情况下处理程序传递错误代码boost :: asio :: error :: operation_aborted。

代码类似:

std::unique_ptr<timer> periodSendTimer_;

ContentQueue::ContentQueue(...timer....): periodSendTimer_(timer){}

ContentQueue::~ContentQueue()
{
    periodSendTimer_->cancel();
}

void ContentQueue::startSendLoop(const boost::system::error_code& error)
{
     if (not error)
        {
            periodSendTimer_->expires_from_now(boost::posix_time::seconds(1));
            periodSendTimer_->async_wait(boost::bind (&ContentQueue::startSendLoop, this, boost::asio::placeholders::error));
        }
        else if(error == boost::asio::error::operation_aborted)
        {.....}
          else
           {....}
}

我的问题是很少发生Destructor的调用 但在同一时间(直到计时器将被取消)计时器到期(来自expires_from_now)并且处理程序startSendLoop未按预期传递boost :: asio :: error :: operation_aborted。

任何反馈/解决方案?

0 个答案:

没有答案