Boost logo

Boost Users :

Subject: [Boost-users] [asio] thread-safe async_write with streambuf
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2009-06-04 03:42:10


Hi all,

I'm looking into thread-safe writing of data using Asio, using a streambuf.
My current implementation boils down to something like the following.

// posting Something from >1 thread(s)
void async_send( Something s ) {
  m_strand.post( async_send_something(s) );
}

// Serialized execution
// Protects resources socket and streambuf
void async_send_something( Something s) {
  std::ostream os( &m_streambuf );
  os << s << std::flush;
  asio::async_write( m_socket, m_streambuf, transfer_all(), bind(
async_complete ) );
}

void async_complete( error e ) {
}

My main confusion is on how to do this with a streambuf. What happens
between async_write and the completion handler? It could be that
async_send_something is called from one thread before a completion handler
of an earlier operation finishes in another thread, and in that case, I'm
not sure if it is defined what happens with the streambuf.

For the situation sketched above, I couldn't find guarantees around the
access pattern of other io_service threads, so my question would be if the
implementation above is really thread-safe, and if not, how to best achieve
that.

Thanks,
Kind regards,

Rutger


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net