title: boost::asio::socket thread safety
body: <p>( This is a simplified version of my original question )</p>
  
  <p>I have several threads that write to a boost asio socket.  This seems to work very well,  with no problems.  </p>
  
  <p>The documentation says a shared socket is not thread safe( <a href="http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/ip__tcp/socket.html" rel="noreferrer">here</a>, way down at the bottom ) so I am wondering if I should protect the socket with mutex, or something.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/6941626/can-i-share-boostasiotcpsocket-object-between-2-threads-that-perform-read-a">question</a> insists that protection is necessary, but gives no advice on how to do so.</p>
  
  <p>All the answers to my original question also insisted that what I was doing dangerous, and most urged me to replace my writes with async_writes or even more complicated things.  However, I am reluctant to do this, since it would complicate code that is already working and none of the answerers convinced me they knew what they ware talking about - they seemed to have read the same documentation as I and were guessing, just as I was.</p>
  
  <p>So, I wrote a simple program to stress test writing to a shared socket from two threads.</p>
  
  <p>Here is the server, which simply writes out whatever it receives from the client</p>
  
  <pre><code>int main()
  {
      boost::asio::io_service io_service;
  
      tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3001));
  
      tcp::socket socket(io_service);
      acceptor.accept(socket);
  
      for (;;)
      {
          char mybuffer[1256];
          int len = socket.read_some(boost::asio::buffer(mybuffer,1256));
          mybuffer[len] = '\0';
          std::cout &lt;&lt; mybuffer;
          std::cout.flush();
  
      }
  
    return 0;
  }
  </code></pre>
  
  <p>Here is the client, which creates two threads that write to a shared socket as fast as they can</p>
  
  <pre><code>boost::asio::ip::tcp::socket * psocket;
  
  void speaker1()
  {
      string msg("speaker1: hello, server, how are you running?\n");
      for( int k = 0; k &lt; 1000; k++ ) {
          boost::asio::write(
              *psocket,boost::asio::buffer(msg,msg.length()));
      }
  
  }
  void speaker2()
  {
      string msg("speaker2: hello, server, how are you running?\n");
      for( int k = 0; k &lt; 1000; k++ ) {
          boost::asio::write(
              *psocket,boost::asio::buffer(msg,msg.length()));
      }
  
  }
  
  int main(int argc, char* argv[])
  {
  
      boost::asio::io_service io_service;
  
    // connect to server
  
      tcp::resolver resolver(io_service);
      tcp::resolver::query query("localhost", "3001");
      tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
      tcp::resolver::iterator end;
      psocket = new tcp::socket(io_service);
      boost::system::error_code error = boost::asio::error::host_not_found;
      while (error &amp;&amp; endpoint_iterator != end)
      {
          psocket-&gt;close();
          psocket-&gt;connect(*endpoint_iterator++, error);
      }
  
  
      boost::thread t1( speaker1 );
      boost::thread t2( speaker2 );
  
      Sleep(50000);
  
  }
  </code></pre>
  
  <p>This works!  Perfectly, as far as I can tell.  The client does not crash.  The messages arrive at the server without garbles.  They usually arrive alternately, one from each thread.  Sometimes one thread get two or three messages in before the other, but I do not think this is a problem so long as there are no garbles and all the messages arrive.</p>
  
  <p>My conclusion: the socket may not be thread safe in some theoretical sense, but it is so hard to make it fail that I am not going to worry about it.</p>