ASYNC_WAIT处理程序班级成员具有取消处理

async_wait handler class member with cancellation handling

本文关键字:处理 成员 取消 WAIT 程序 ASYNC      更新时间:2023-10-16

这很好:

class cStartSequence
{
void Tick()
{
// do something
}
void Wait()
{    
    myTimer->expires_from_now( 
        boost::posix_time::seconds( mySecs ) );
    myTimer->async_wait(boost::bind(
                            &cStartSequence::Tick,
                            this
                               ));
}
 ...
};

我希望能够取消计时器并让处理程序做一些不同的事情

void Tick( boost::system::error_code & ec )
{
  if( ! ec )
     // do something
  else
    // do something different
}

问题是如何修改呼叫async_wait?

这不编译

myTimer->async_wait(boost::bind(
                        &cStartSequence::Tick,
                        this,
                        boost::asio::placeholders::error
                         ));

编译器投诉:

C:UsersJamescodeboostv1_63boostbindbind.hpp|319|error:
  no match for call to 
  '(boost::_mfi::mf1<void, pinmed::wrs::cStartSequence, boost::system::error_code&>) 
 (pinmed::wrs::cStartSequence*&, const boost::system::error_code&)'

我在Async_wait参数上尝试了一些变体,但没有运气。

Boost:ASIO V1.63,Windows 10,Code :: Blocks V16.01

您的tick方法获取非const参考。没关系(并且不满足处理程序要求)。使用

void Tick(boost::system::error_code const& ec);

或也许

void Tick(boost::system::error_code ec);

出于略有模糊的原因(特定于图书馆的优化IIRC),ASIO作者首选第一个。

ps。有关安全取消截止时间计时器,请参见

  • 取消deadline_timer,无论如何都触发回调
  • 与悬挂式的Coroutine正确清理