我什么时候必须使用 boost::asio:strand

2021-12-24 00:00:00 multithreading c++ boost boost-asio

看了boost::asio的文档,还是不清楚什么时候需要用到asio::strand.假设我有一个使用 io_service 的线程,那么在套接字上写入如下是否安全?

Reading the document of boost::asio, it is still not clear when I need to use asio::strand. Suppose that I have one thread using io_service is it then safe to write on a socket as follows ?

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;
    }
  }
}

多线程调用 Connection::write(..) 或者我必须使用 asio::strand 吗?

Where multiple threads calls Connection::write(..) or do I have to use asio::strand ?

推荐答案

简短回答:不,在这种情况下您不必使用 strand.

Short answer: no, you don't have to use a strand in this case.

广泛简化,io_service 包含一个函数对象(处理程序)列表.当在服务上调用 post() 时,处理程序被放入列表中.例如每当异步操作完成时,处理程序及其参数都会放入列表中.io_service::run() 一个接一个地执行处理程序.因此,如果只有一个线程调用 run() 就像您的情况一样,则没有同步问题并且不需要 strand.
只有多个线程在同一个io_service上调用run(),多个handlers才会同时执行,N个线程最多N个并发handlers.如果这是一个问题,例如如果队列中可能有两个处理程序同时访问同一个对象,则需要 strand.
您可以将 strand 视为一组处理程序的一种锁.如果线程执行与 strand 关联的处理程序,该 strand 将被锁定,并在处理程序完成后释放.任何其他线程只能执行与锁定的 strand 无关的处理程序.

Broadly simplificated, an io_service contains a list of function objects (handlers). Handlers are put into the list when post() is called on the service. e.g. whenever an asynchronous operation completes, the handler and its arguments are put into the list. io_service::run() executes one handler after another. So if there is only one thread calling run() like in your case, there are no synchronisation problems and no strands are needed.
Only if multiple threads call run() on the same io_service, multiple handlers will be executed at the same time, in N threads up to N concurrent handlers. If that is a problem, e.g. if there might be two handlers in the queue at the same time that access the same object, you need the strand.
You can see the strand as a kind of lock for a group of handlers. If a thread executes a handler associated to a strand, that strand gets locked, and it gets released after the handler is done. Any other thread can execute only handlers that are not associated to a locked strand.

注意:这个解释可能过于简单化,技术上也不准确,但它给出了io_service中发生的事情的基本概念和 strands.

Caution: this explanation may be over-simplified and technically not accurate, but it gives a basic concept of what happens in the io_service and of the strands.

相关文章