How to serializing access a udp::socket object in multiple threads (in boost/asio)?

61 views Asked by At

if i use strand to achieve my purpose:

method 1:

co_spawn(strand, []() -> awaitable<void> {
        co_await a_socket.async_receive_from(...., use_awaitable);
      }, detached);

or:

co_await co_spawn(strand, []() -> awaitable<void> {
        co_await a_socket.async_receive_from(...., use_awaitable);
      }, use_awaitable);

method 2:

co_await a_socket.async_receive_from(...., asio::bind_executor(strand, use_awaitable));

method 3:

udp::socket socket(strand); // construct
    co_await a_socket.async_receive_from(...., use_awaitable);

which is the right method?

0

There are 0 answers