1

Meta

I know this looks like a duplicate of asio : multiple pending async_read? but is not, since the OP there was actually asking about async_read not async_read_some which he/she also said in the comment . Also in contrast to his or her question I can only find that multiple async_reads are disallowed. Neither the async_read_some documentation of TCP sockets nor serial ports are mentioning anything about it.

Question

Are multiple async_read_some calls disallowed? If not, I would assume to the dokumented proactor behaviour of boost asio, that I something was read, the handler of the first posted async_read_some would be executed, and only after the next read, the second handler in a FIFO queue concept. (Assuming we would not have 0 reads). If it is disallowed, could somebody help me out with the pointer to the documentation?

Superlokkus
  • 4,731
  • 1
  • 25
  • 57

1 Answers1

2

No, multiple async_read_some operations are not disallowed. It's pretty useless

You have to synchronize the access to the socket object (it is not thread safe) and buffers used.

Also, the order in which the async operations complete is not guaranteed. Due to the non-deterministic way in which packets are split on the network layers, it can be pretty hard to usefully interpret all the fragments "simultaneously" received. This might not be an issue, e.g. if you're just doing a histogram of unique octets received, but that's a contrived example.

The order in which handlers are initiated can be influenced (see https://stackoverflow.com/a/19963481/85371).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks, yeah I know I was between a rock and a hard place: I have to implement Modbus, which has some sort of collision avoidance at start. So you start a timer and start to listen i.e. receive for characters on bus. If you receive before timer expire, you reset the timer, if the timer expires you dont want to consume characters anymore with that handler but the one from your main idle code. Well I just cancel the async_read_some now, and got by experimentation to post the following cont, to give the cancel handler a chance, to acutally cancel. – Superlokkus Aug 30 '19 at 22:23
  • So basically I was just curious about the documented and intended makings of asio. The 2 active async reads where just a question that poped up while thinking about possible (but also not all good) solutions. – Superlokkus Aug 30 '19 at 22:27