Discussion:
[asio-users] Concurrent read/write operations?
Brian Hassink
2016-12-15 12:52:12 UTC
Permalink
Hi,

It's been about ten years since I last used ASIO, and I've forgotten much in that time.

I was wanting to clarify whether concurrent read/write operations were supported? That is, one thread may be reading from a socket, while another may be writing to the same socket at the same time.

I realize this requires some cleanup coordination when there is a connection failure (e.g., peer disconnect) and both threads see an error.

Thanks,
Brian

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Tatsuyuki Ishi
2016-12-15 12:56:39 UTC
Permalink
They should be internally safe, however doing cancel is probably not. So,
anyway you should synchronize on it.
Post by Brian Hassink
Hi,
It's been about ten years since I last used ASIO, and I've forgotten much in that time.
I was wanting to clarify whether concurrent read/write operations were
supported? That is, one thread may be reading from a socket, while another
may be writing to the same socket at the same time.
I realize this requires some cleanup coordination when there is a
connection failure (e.g., peer disconnect) and both threads see an error.
Thanks,
Brian
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Allen
2016-12-15 13:05:48 UTC
Permalink
Yes, concurrent reading and writing works fine. You just have to make
sure you don't interleave writes, i.e., you have to wait until a prior
write calls its completion handler before you initiate another write.
If you do interleave writes, it may work if all writes are short, but
with longer writes, the data on the other end will arrive interleaved.
Post by Brian Hassink
Hi,
It's been about ten years since I last used ASIO, and I've forgotten much in that time.
I was wanting to clarify whether concurrent read/write operations were supported? That is, one thread may be reading from a socket, while another may be writing to the same socket at the same time.
I realize this requires some cleanup coordination when there is a connection failure (e.g., peer disconnect) and both threads see an error.
Thanks,
Brian
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Dale Wilson
2016-12-15 14:43:24 UTC
Permalink
Post by Allen
Yes, concurrent reading and writing works fine. You just have to make
sure you don't interleave writes, i.e., you have to wait until a prior
write calls its completion handler before you initiate another write.
It's also not a good idea to interleave reads! You won't be sure which
reader will receive which data. This can be very exciting on TCP (grin).

What DOES work is having one reader and one writer where both the reader
and writer are good asynchronous citizens that start the next I/O (if
possible) from the termination handler of the previous I/O so at most one
read and one write request is outstanding at any time.

Dale
Marat Abrarov
2016-12-15 17:15:54 UTC
Permalink
Just my 2c:



Note that it depends on OS (on the underlying layer wrapped by Asio) and on Windows (at least) access to socket descriptor should be synchronized, i.e. functions (API) provided by WinSock are not thread-safe in case of concurrent usage of the same socket descriptor. Async read (receive) can be performed in parallel with async write (send), but access to socket descriptor (and thus to the instance of socket class provided by Asio) should be synchronized when these 2 operations are initiated (started).



Regards,

Marat Abrarov.
Brian Hassink
2016-12-15 13:09:00 UTC
Permalink
Thanks for the reply.

I should clarify that the question is in the context of async operations, where my application instructs ASIO to perform both an async read and async write. Assuming the socket is both ready to read and write, will ASIO perform these operations concurrently with each other using two threads, or serially?

-Brian

----- Original Message -----
From: ***@gmail.com
To: asio-***@lists.sourceforge.net
Sent: Thursday, December 15, 2016 7:57:25 AM GMT -05:00 US/Canada Eastern
Subject: Re: [asio-users] Concurrent read/write operations?


They should be internally safe, however doing cancel is probably not. So, anyway you should synchronize on it.


2016幎12月15日(朚) 21:55 Brian Hassink < ***@oracle.com >:


Hi,

It's been about ten years since I last used ASIO, and I've forgotten much in that time.

I was wanting to clarify whether concurrent read/write operations were supported? That is, one thread may be reading from a socket, while another may be writing to the same socket at the same time.

I realize this requires some cleanup coordination when there is a connection failure (e.g., peer disconnect) and both threads see an error.

Thanks,
Brian

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

_______________________________________________ asio-users mailing list asio-***@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/asio-users _______________________________________________ Using Asio? List your project at http://think-async.com/Asio/WhoIsUsingAsio
Allen
2016-12-15 13:49:27 UTC
Permalink
Post by Brian Hassink
I should clarify that the question is in the context of async operations,
where my application instructs ASIO to perform both an async read and async
write. Assuming the socket is both ready to read and write, will ASIO
perform these operations concurrently with each other using two threads, or
serially?
Concurrently, at least on Windows 7 and higher--I can't say definitely
what will happen on other OS's. Note that you don't actually need two
threads, only one will do, because ASIO async reads and writes uses
non-blocking IO calls.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Brian Hassink
2016-12-15 13:22:46 UTC
Permalink
I think I found the answer here (section 6), in the brief discussion on "one io_service, multiple threads".

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3388.pdf

It appears the read and write completion handlers can be called concurrently, which I assume means the socket operations are concurrent, unless I explicitly use a "strand" to serialize them.

Would that understanding be correct?

-Brian

----- Original Message -----
From: ***@oracle.com
To: asio-***@lists.sourceforge.net
Sent: Thursday, December 15, 2016 8:09:00 AM GMT -05:00 US/Canada Eastern
Subject: Re: [asio-users] Concurrent read/write operations?



Thanks for the reply.

I should clarify that the question is in the context of async operations, where my application instructs ASIO to perform both an async read and async write. Assuming the socket is both ready to read and write, will ASIO perform these operations concurrently with each other using two threads, or serially?

-Brian

----- Original Message -----
From: ***@gmail.com
To: asio-***@lists.sourceforge.net
Sent: Thursday, December 15, 2016 7:57:25 AM GMT -05:00 US/Canada Eastern
Subject: Re: [asio-users] Concurrent read/write operations?


They should be internally safe, however doing cancel is probably not. So, anyway you should synchronize on it.


2016幎12月15日(朚) 21:55 Brian Hassink < ***@oracle.com >:


Hi,

It's been about ten years since I last used ASIO, and I've forgotten much in that time.

I was wanting to clarify whether concurrent read/write operations were supported? That is, one thread may be reading from a socket, while another may be writing to the same socket at the same time.

I realize this requires some cleanup coordination when there is a connection failure (e.g., peer disconnect) and both threads see an error.

Thanks,
Brian

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

_______________________________________________ asio-users mailing list asio-***@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/asio-users _______________________________________________ Using Asio? List your project at http://think-async.com/Asio/WhoIsUsingAsio
David Schwartz
2016-12-15 17:26:22 UTC
Permalink
Post by Brian Hassink
I think I found the answer here (section 6), in the brief discussion on "one
io_service, multiple threads".
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3388.pdf
It appears the read and write completion handlers can be called
concurrently, which I assume means the socket operations are concurrent,
unless I explicitly use a "strand" to serialize them.
Would that understanding be correct?
Yes. But be warned, things can break internally if you don't use a
strand (or something comparable) to serialize them. For example, if
the read and the write are both to the same SSL connection, the
internal composed operations that form the read and write operation
may corrupt the SSL connection's state information. You have to have
deep knowledge of exactly what you're making ASIO do internally and
how composed operations are dispatched if you're not going to either
use a strand or use a single thread.

A better idea is usually to use a strand for the read and write
operations and, if you need read/write concurrency, dispatch the
handling of higher-level functions to your own code running in its own
context.

DS

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Brian Hassink
2016-12-15 18:20:24 UTC
Permalink
Thanks for your response, and that of everyone else as well.

I have run across the thread-safety issue with OpenSSL in the past, and agree such sockets would necessitate a "strand".

-Brian


----- Original Message -----
From: ***@gmail.com
To: asio-***@lists.sourceforge.net
Sent: Thursday, December 15, 2016 12:27:02 PM GMT -05:00 US/Canada Eastern
Subject: Re: [asio-users] Concurrent read/write operations?
Post by Brian Hassink
I think I found the answer here (section 6), in the brief discussion on "one
io_service, multiple threads".
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3388.pdf
It appears the read and write completion handlers can be called
concurrently, which I assume means the socket operations are concurrent,
unless I explicitly use a "strand" to serialize them.
Would that understanding be correct?
Yes. But be warned, things can break internally if you don't use a
strand (or something comparable) to serialize them. For example, if
the read and the write are both to the same SSL connection, the
internal composed operations that form the read and write operation
may corrupt the SSL connection's state information. You have to have
deep knowledge of exactly what you're making ASIO do internally and
how composed operations are dispatched if you're not going to either
use a strand or use a single thread.

A better idea is usually to use a strand for the read and write
operations and, if you need read/write concurrency, dispatch the
handling of higher-level functions to your own code running in its own
context.

DS

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

Loading...