Wann muss ich die Verwendung von boost::asio:strand

Lesen das Dokument von boost::asio, es ist noch nicht klar, Wann muss ich die Verwendung von asio::Strang. Angenommen, ich habe einen thread mit io_service ist es dann sicher schreiben auf einen socket wie folgt ?

void Connection::write(boost::shared_ptr<string> msg)
{
    _io_service.post(boost::bind(&Connection::_do_write,this,msg));
}

void Connection::_do_write(boost::shared_ptr<string> msg)
{
    if(_write_in_progress)
    {
      _msg_queue.push_back(msg);
    }
    else
    {
      _write_in_progress=true;
      boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
      boost::bind(&Connection::_handle_write,this,
             boost::asio::placeholders::error));
    }
}

void Connection::_handle_write(boost::system::error_code const &error)
{
  if(!error)
  {
    if(!_msg_queue.empty())
    {
          boost::shared_ptr<string> msg=_msg_queue.front();
      _msg_queue.pop_front();
      boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
           boost::bind(&Connection::_handle_write,this,
                   boost::asio::placeholders::error));
        }
    else
    {
      _write_in_progress=false;
    }
  }
}

Wo mehrere threads Aufrufe Anschluss::write(..) oder muss ich die Verwendung von asio::Strang ?

InformationsquelleAutor user2004360 | 2013-05-06
Schreibe einen Kommentar