0

This question is similar to this question asked about POSIX threads, but I would like to know what the answer is for boost::asio ip::tcp::socket:

  1. Can we call send from one thread and recv from another on the same socket?

In a C++11 application I'd like to share a tcp::socket such that one thread does blocking socket.read_some() and processes incoming data and another thread shares the same tcp::socket and only performs boost::asio::write() calls. Will this be thread-safe?

phoenix
  • 7,988
  • 6
  • 39
  • 45
  • 1
    As it stated in Asio documentation, 'socket' object is not thread-safe. – Igor R. Jun 15 '18 at 11:07
  • Slightly related, but more complicated: [sync tcp::iostream in a multi-threaded context has the same issue](https://stackoverflow.com/questions/48117493/how-to-avoid-data-race-with-asioiptcpiostream/48123713#48123713). There's no way to "thread synchronize" your way out of that, so I came up with a hack based on **not sharing** the the `socket` object. Perhaps when you've read the docs, this example will help you solidify your understanding. – sehe Jun 15 '18 at 12:39

0 Answers0