开发者

is it valid to async send data before completion handler of the previous one was invoked?

开发者 https://www.devze.com 2023-04-12 16:50 出处:网络
I\'m sending data asynchronously to TCP socket. Is it valid to send the next data piece before the previous one was reported as sent by completion handler?

I'm sending data asynchronously to TCP socket. Is it valid to send the next data piece before the previous one was reported as sent by completion handler?

As I know it's not allowed when sending is done from different threads. In my case all sending are done from the same thread.

Different modules of my client send data to the same socket. E.g. module1 sent some data and will continue when co开发者_运维知识库rresponding completion handler is invoked. Before this io_service invoked deadline_timer handler of module2 which leads to another async_write call. Should I expect any problems here?


Is it valid to send the next data piece before the previous one was reported as sent by completion handler?

No it is not valid to interleave write operations. This is very clear in the documentation

This operation is implemented in terms of zero or more calls to the stream's async_write_some function, and is known as a composed operation. The program must ensure that the stream performs no other write operations (such as async_write, the stream's async_write_some function, or any other composed operations that perform writes) until this operation completes.

emphasis added by me.

As I know it's not allowed when sending is done from different threads. In my case all sending are done from the same thread.

Your problem has nothing to do with threads.


Yes, you can do that as long as the underlying memory (buffer) is not modified until the write handler is called. Calling async_write means you hand over the buffer ownership to Asio. When the write handler is called, the buffer ownership is given back to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号