O'Reilly logo

Boost.Asio C++ Network Programming by John Torjo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asynchronous operations

Boost.Asio also allows you to run any of your functions asynchronously. Just use the following code snippet:

void my_func() {
   ...
}
service.post(my_func);

That will make sure that my_func is called in one of the threads that called service.run(). You can also run a function asynchronously and have a completion handler, which will notify you when the function completes. The pseudocode would look like the following code snippet:

void on_complete() {
   ...
}
void my_func() {
    ...
    service.post(on_complete);
}
async_call(my_func);

There is no async_call function, however, you'll have to create your own. Fortunately, it's not that hard. Refer to the following code snippet:

struct async_op : boost::enable_shared_from_this<async_op>, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required