mercredi 30 novembre 2022

boost::asio Strictly sequential invocation of event handlers using strand

I have a question regarding to the usage of strand in boost::asio framework. The manuals refer the following

In the case of composed asynchronous operations, such as async_read() or async_read_until(), if a completion handler goes through a strand, then all intermediate handlers should also go through the same strand. This is needed to ensure thread safe access for any objects that are shared between the caller and the composed operation (in the case of async_read() it's the socket, which the caller can close() to cancel the operation). This is done by having hook functions for all intermediate handlers which forward the calls to the customisable hook associated with the final handler:

Let's say that we have the following example Strand runs in a async read socket operation . Socket read the data and forwards them to a async writer socket. Two operation are in the same io_service. Is this write operation thread safe as well?Is is called implicity in the same strand? Or is it needed to explicitly call async_write in the strand

read_socket.async_read_some(my_buffer,
    boost::asio::bind_executor(my_strand,
      [](error_code ec, size_t length)
      {
           write_socket.async_write_some(boost::asio::buffer(data, size), handler);
      }));

Is the async_write_some sequential executing in the following example or needs strand as well?

Aucun commentaire:

Enregistrer un commentaire