48

我正在查看Boost 网站上的HTTP Server 3示例。

你们能解释一下为什么我需要strand每个连接吗?正如我所看到的,我们read_some只在读取事件的处理程序中调用。所以基本上read_some调用是顺序的,因此不需要 strand (第 3 段的第 2 项说同样的事情)。多线程环境的风险在哪里?

4

2 回答 2

100

文档是正确的。使用半双工协议实现,例如HTTP Server 3strand则不需要。调用链可以说明如下:

void connection::start()
{
  socket.async_receive_from(..., &handle_read);  ----.
}                                                    |
    .------------------------------------------------'
    |      .-----------------------------------------.
    V      V                                         |
void connection::handle_read(...)                    |
{                                                    |
  if (result)                                        |
    boost::asio::async_write(..., &handle_write); ---|--.
  else if (!result)                                  |  |
    boost::asio::async_write(..., &handle_write);  --|--|
  else                                               |  |
    socket_.async_read_some(..., &handle_read);  ----'  |
}                                                       |
    .---------------------------------------------------'
    |
    V
void handle_write(...)

如图所示,每个路径只启动一个异步事件。由于不可能在 上同时执行处理程序或操作socket_,因此据说它在隐式链中运行。


线程安全

虽然它在示例中并未作为问题出现,但我想强调 strands 和组合操作的一个重要细节,例如boost::asio::async_write. 在解释细节之前,让我们先介绍一下 Boost.Asio 的线程安全模型。对于大多数 Boost.Asio 对象,在一个对象上挂起多个异步操作是安全的;它只是指定对对象的并发调用是不安全的。在下图中,每一列代表一个线程,每一行代表一个线程在某一时刻正在做什么。

单个线程进行顺序调用是安全的,而其他线程不进行:

线程_1 | 线程_2
--------------------------------------------------+------------ ----------------------------
socket.async_receive(...); | ...
socket.async_write_some(...); | ...

多个线程进行调用是安全的,但不能同时调用:

线程_1 | 线程_2
--------------------------------------------------+------------ ----------------------------
socket.async_receive(...); | ...
... | socket.async_write_some(...);

但是,多个线程同时进行调用是不安全的1

线程_1 | 线程_2
--------------------------------------------------+------------ ----------------------------
socket.async_receive(...); | socket.async_write_some(...);
... | ...

股线

为了防止并发调用,处理程序通常从链中调用。这是通过以下任一方式完成的:

组合操作的独特之处在于对流的中间调用是在处理程序的 strand调用的,如果存在的话,而不是在其中启动组合操作的 strand。与其他操作相比,这呈现了指定链的位置的反转。这是一些专注于 strand 使用的示例代码,它将演示通过非组合操作读取的套接字,并与组合操作同时写入。

void start()
{
  // Start read and write chains.  If multiple threads have called run on
  // the service, then they may be running concurrently.  To protect the
  // socket, use the strand.
  strand_.post(&read);
  strand_.post(&write);
}

// read always needs to be posted through the strand because it invokes a
// non-composed operation on the socket.
void read()
{
  // async_receive is initiated from within the strand.  The handler does
  // not affect the strand in which async_receive is executed.
  socket_.async_receive(read_buffer_, &handle_read);
}

// This is not running within a strand, as read did not wrap it.
void handle_read()
{
  // Need to post read into the strand, otherwise the async_receive would
  // not be safe.
  strand_.post(&read);
}

// The entry into the write loop needs to be posted through a strand.
// All intermediate handlers and the next iteration of the asynchronous write
// loop will be running in a strand due to the handler being wrapped.
void write()
{
  // async_write will make one or more calls to socket_.async_write_some.
  // All intermediate handlers (calls after the first), are executed
  // within the handler's context (strand_).
  boost::asio::async_write(socket_, write_buffer_,
                           strand_.wrap(&handle_write));
}

// This will be invoked from within the strand, as it was a wrapped
// handler in write().
void handle_write()
{
  // handler_write() is invoked within a strand, so write() does not
  // have to dispatched through the strand.
  write();
}

处理程序类型的重要性

此外,在组合操作中,Boost.Asio 使用参数相关查找(ADL) 通过完成处理程序的链调用中间处理程序。因此,完成处理程序的类型具有适当的asio_handler_invoke()挂钩很重要。如果类型擦除发生在没有适当asio_handler_invoke()钩子的类型上,例如boost::function从 的返回类型构造a 的情况strand.wrap,则中间处理程序将在链外执行,并且只有完成处理程序将在链内执行。有关更多详细信息,请参阅答案。

在以下代码中,所有中间处理程序和完成处理程序都将在链中执行:

boost::asio::async_write(stream, buffer, strand.wrap(&handle_write));

在下面的代码中,只有完成处理程序将在 strand 中执行。没有任何中间处理程序将在链中执行:

boost::function<void()> handler(strand.wrap(&handle_write));
boost::asio::async_write(stream, buffer, handler);

1.修订历史记录了此规则的异常情况。如果操作系统支持,同步读取、写入、接受和连接操作是线程安全的。为了完整起见,我将其包含在此处,但建议谨慎使用。

于 2012-10-09T13:22:21.443 回答
8

我相信这是因为组合操作async_writeasync_write由多个socket::async_write_some异步组成。Strand 有助于序列化这些操作。asio 的作者 Chris Kohlhoff 在 1:17 左右的boostcon 演讲中简短地谈到了它。

于 2012-10-09T07:09:47.780 回答