ravenspoint
ravenspoint

Reputation: 20457

async_wait handler class member with cancellation handling

This works well:

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
                               ));
}
 ...
};

I want to be able to cancel the timer and have the handler do something different

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

The question is how to modify the call to async_wait?

This does not compile

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

compiler complaint:

C:\Users\James\code\boost\v1_63\boost\bind\bind.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&)'

I tried some variations on the async_wait parameters, but no luck.

boost:asio v1.63, windows 10, code::blocks v16.01

Upvotes: 4

Views: 501

Answers (1)

sehe
sehe

Reputation: 392833

Your Tick method takes a non-const reference. That's not ok (and doesn't satisfy the handler requirements). Use either

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

Or maybe

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

The first one is preferred by Asio author(s) for slightly obscurantist reasons (library-specific optimizations IIRC)

PS. For safe cancellation of deadline timers see also

Upvotes: 2

Related Questions