[go: up one dir, main page]

0% found this document useful (0 votes)
367 views16 pages

Boost - Asio - Asynchronous IO

This document discusses asynchronous I/O using the Boost.Asio library. It explains what asynchronous operations are, when to use Boost.Asio instead of threads, and how to structure asynchronous programs using Boost.Asio. Key aspects covered include Boost.Asio's architecture of I/O services and objects, writing asynchronous handlers, running the I/O service, and synchronizing operations across threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views16 pages

Boost - Asio - Asynchronous IO

This document discusses asynchronous I/O using the Boost.Asio library. It explains what asynchronous operations are, when to use Boost.Asio instead of threads, and how to structure asynchronous programs using Boost.Asio. Key aspects covered include Boost.Asio's architecture of I/O services and objects, writing asynchronous handlers, running the I/O service, and synchronizing operations across threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Asynchronous I/O with

Boost.Asio
What are asynchronous operations?
When shall I use Boost.Asio (and not threads)?
How do I use Boost.Asio?
How does Boost.Asios architecture look like?
What else do I need to know about Boost.Asio?
Boris Schling, boris@highscore.de
Meeting C++, Dsseldorf, 10 November 2012

Asynchronous operations
do_sync()
do_async (&done)
event_loop()
done()
Your code
Asynchronous I/O with Boost.Asio

A library like Boost.Asio


2

Boost.Asio vs. Boost.Thread


Use asynchronous operations to access resources concurrently outside of your process

do_async (&done)
event_loop()
done()
Your code
Asynchronous I/O with Boost.Asio

A library like Boost.Asio


3

Boost.Asio vs. Boost.Thread


Use threads to access resources
concurrently in your process

block()

Your code
Asynchronous I/O with Boost.Asio

Boost.Asio sample program

void handler(const boost::system::error_code &ec) {}


int main()
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service,
boost::posix_time::seconds(5));
timer.async_wait(handler);
io_service.run();
}

Asynchronous I/O with Boost.Asio

Boost.Asio sample program

boost::asio::deadline_timer timer(io_service,
boost::posix_time::seconds(5));
timer.async_wait(handler);
io_service.run();

deadline_timer is one of many classes


in Boost.Asio which make it possible
to call functions asynchronously
async_wait() returns immediately

run() calls handler after 5 seconds


Asynchronous I/O with Boost.Asio

Asynchronous operations
deadline_timer

async_wait() to wait until some time is expired

ip::tcp::acceptor

async_accept() to accept TCP/IP connections

ip::tcp::resolver

async_resolve() to resolve hostnames

ip::tcp::socket

async_read_some() and async_write_some()


to send and receive data

Boost.Asio provides different classes which turn


different blocking functions into asynchronous functions
Asynchronous I/O with Boost.Asio

Boost.Asio terminology

void handler(const boost::system::error_code &ec) {}


int main()
I/O service object
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service,
boost::posix_time::seconds(5));
I/O object
timer.async_wait(handler);
io_service.run();
}

Asynchronous I/O with Boost.Asio

Boost.Asio terminology
deadline_timer

io_service

An I/O object is initialized


with an I/O service object.
It doesnt use the I/O service
object though it uses
services provided by the I/O
service object.

An I/O service object


provides services to I/O
objects. Think of it as a set of
services set because there
is maximum one instance of
each and every service.

While I/O objects and I/O service objects are


visible in user code, I/O services do the hard work
in the background.
Asynchronous I/O with Boost.Asio

Boost.Asio architecture
I/O service A

I/O service B

I/O service object

I/O object 1

Asynchronous I/O with Boost.Asio

I/O object 2

I/O object 3

10

Boost.Asio architecture
boost::asio::
stream_socket_service

boost::asio::
deadline_timer_service

boost::asio::
io_service

boost::asio::ip::tcp::
basic_stream_socket

Asynchronous I/O with Boost.Asio

boost::asio::ip::tcp::
basic_socket_iostream

boost::asio::
basic_deadline_timer

11

Multiple async operations

...
boost::asio::io_service io_service;
boost::asio::deadline_timer timer1(io_service,
boost::posix_time::seconds(5));
boost::asio::deadline_timer timer2(io_service,
boost::posix_time::seconds(5));
timer1.async_wait(handler);
timer2.async_wait(handler);
io_service.run();
...

Asynchronous I/O with Boost.Asio

12

Boost.Asio with threads

...
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service,
boost::posix_time::seconds(5));
timer.async_wait(handler);
boost::thread t([&io_service]{ io_service.run(); });
io_service.run();
t.join();
...

Asynchronous I/O with Boost.Asio

13

Synchronization with strand

...
boost::asio::io_service::strand strand(io_service);
timer1.async_wait(strand.wrap(handler));
timer2.async_wait(strand.wrap(handler));
boost::thread t([&io_service]{ io_service.run(); });
io_service.run();
t.join();
...

Asynchronous I/O with Boost.Asio

14

No return with work

...
boost::asio::io_service io_service;
boost::asio::io_service::work work(io_service);
io_service.run();
...

Asynchronous I/O with Boost.Asio

15

More information
Boost.Asio documentation:
http://www.boost.org/libs/asio/

BoostCon presentations:
http://boostcon.boost.org/presentations/
Thinking asynchronously (Chris Kohlhoff, 2011)
Creating Boost.Asio extensions (Boris Schling, 2011)

Online book:
http://en.highscore.de/cpp/boost/asio.html
http://www.highscore.de/cpp/boost/asio.html (German)
http://zh.highscore.de/cpp/boost/asio.html (Chinese)

JTC1/SC22/WG21 paper N3388:


http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3388.pdf

50 Boost C++ Libraries in 180 minutes

16

You might also like