Re: [asio-users] thread-safe async_write with streambuf
Brought to you by:
chris_kohlhoff
From: Scott G. <sgi...@su...> - 2009-06-10 22:02:10
|
Derek Taylor <dt...@da...> writes: [...] > I haven't found any asio feature which will guarantee ordering on these > accesses for multiple sender threads. Short of that, I was about to set out to > write the code to maintain outgoing streambufs in a pending list when previous > writes haven't completed. The completion handler can check this list and call > async_write on the next item in the list if it's not empty. Of course, this > all has to be written in a thread safe way, and for that I'll use > locks. I implemented something very similar, and found that it worked reliably and performance was fine. Asio can actually write a whole vector of buffers at once, so I simply kept a vector of buffers waiting to be written, and when I flushed the write buffer I would ask asio to send the entire vector, then start working in a new vector. I tried doing the same thing by appending to buffers directly instead of vectors of buffers, but found performance wasn't much better, and it was a little harder to manage. I have some test programs I used to implement this lying around, I can dig them out if you'd like to take a look. ----Scott. |