Boost logo

Boost Users :

Subject: Re: [Boost-users] Which socket read get first when both asio::async_read and asio::read are active?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-08-02 02:38:48

On 2/08/2017 13:30, jupiter wrote:
> I have a socket server program to activate in asio::async_read for
> receive any messages initiated from client. But when the server sends a
> message to the client, it uses sync write and flowing a sync read
> (asio::read) to immediately receive the response. The issue I can think
> of is who will get first read when both asio::async_read and asio::read
> are active?

One or both of them selected at semi-random by the OS. Basically: don't
do that.

(Especially because both read and async_read are not necessarily single
operations -- they can internally be composed of multiple calls to

Each individual data block will only be received by at most one of the
handlers, but a single "response" could come in as arbitrarily many
blocks, and if you have multiple read operations pending simultaneously
then the chances of multiple blocks hitting multiple handlers are nearly

You will need to remove the sync read and only do the async read; your
async read handler (or downstream code) will need to recognise the
difference between an unsolicited message and a response and handle it

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at