title: boost asio async_write : how to not interleaving async_write calls?
body: <p>Here's my implementation :</p>
  
  <ul>
  <li>Client A send a message for Client B</li>
  <li>Server process the message by <code>async_read</code> the right amount of data and
  will wait for new data from Client A (in Order not to block Client A)</li>
  <li>Afterwards Server will process the information (probably do a mysql
  query) and then send the message to Client B with <code>async_write</code>.</li>
  </ul>
  
  <p>The problem is, if Client A send message really fast, <code>async_writes</code> will interleave before the previous async_write handler is called.</p>
  
  <p>Is there a simple way to avoid this problem ?</p>
  
  <p>EDIT 1 : 
  If a Client C sends a message to Client B just after Client A, the same issue should appear... </p>
  
  <p>EDIT 2 : 
  This would work ? because it seems to block, I don't know where...</p>
  
  <pre><code> namespace structure {                                                              
    class User {                                                                     
    public:                                                                          
      User(boost::asio::io_service&amp; io_service, boost::asio::ssl::context&amp; context) :
        m_socket(io_service, context), m_strand(io_service), is_writing(false) {}    
  
      ssl_socket&amp; getSocket() {                                                      
        return m_socket;                                                             
      }                                                                              
  
      boost::asio::strand getStrand() {                                              
        return m_strand;                                                             
      }                                                                              
  
      void push(std::string str) {                                                   
        m_strand.post(boost::bind(&amp;structure::User::strand_push, this, str));        
      }                                                                              
  
      void strand_push(std::string str) {                                            
  
        std::cout &lt;&lt; "pushing: " &lt;&lt; boost::this_thread::get_id() &lt;&lt; std::endl;       
        m_queue.push(str);                                                           
        if (!is_writing) {                                                           
          write();                                                                   
          std::cout &lt;&lt; "going to write" &lt;&lt; std::endl;                                
        }                                                                            
        std::cout &lt;&lt; "Already writing" &lt;&lt; std::endl;                                 
      }                                                                              
  
      void write() {                                                                 
        std::cout &lt;&lt; "writing" &lt;&lt; std::endl;                                         
        is_writing = true;                                                           
        std::string str = m_queue.front();                                           
        boost::asio::async_write(m_socket,                                           
                                 boost::asio::buffer(str.c_str(), str.size()),       
                                 boost::bind(&amp;structure::User::sent, this)           
                                 );                                                  
      }                                                                              
  
      void sent() {                                                                  
        std::cout &lt;&lt; "sent" &lt;&lt; std::endl;                                            
        m_queue.pop();                                                               
        if (!m_queue.empty()) {                                                      
          write();                                                                   
          return;                                                                    
        }                                                                            
        else                                                                         
          is_writing = false;                                                        
        std::cout &lt;&lt; "done sent" &lt;&lt; std::endl;                                       
      }                                          
  
    private:                                     
      ssl_socket          m_socket;              
      boost::asio::strand m_strand;              
      std::queue&lt;std::string&gt;     m_queue;       
      bool                        is_writing;    
    };                                           
  }                                              
  
  #endif
  </code></pre>