rank1
rank1

Reputation: 1038

How to call boost::asio async functions

I want to use boost::asio deadline_timer on my asynchronous call. I want to call function A::fun(0) on calling object. To do I do sth like that:

io_service io;
deadline_timer timer(io, boost::posix_time::seconds(10));
timer.async_wait(&A::fun, this, 0); //error
io.run();

In the second line I got error : basic_deadline_timer::async_wait(const WaitHandler &)' : expects 1 arguments - 2 provide.

I used the same syntax as for starting boost::threads on some tasks, and I thought it would work.

Upvotes: 0

Views: 1589

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254431

You need to pass a single function object which meets the WaitHandler requirements; that is, which accepts an error code as its argument.

In C++11 I'd use a lambda:

timer.async_wait([this](const boost::system::error_code &){fun(0);});

In C++03, use Boost.Bind to make a function object:

timer.async_wait(bind(&A::fun, this, 0));

In either case, you probably want to check the error code that was passed (in the second case, by modifying or wrapping fun) to make sure that the timer really has expired.

Upvotes: 3

Related Questions