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 asasync_write
, the stream'sasync_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.
精彩评论