问题
Is it safe to call async_write & async_read from different threads in situation when io_service::run() was called from only one thread? Thank you!
回答1:
Is it safe to call
async_write
&async_read
from different threads
Yes, but with a caveat. You can safely do this for distinct socket
objects
Thread Safety
Distinct objects: Safe.
Shared objects: Unsafe.
回答2:
The documentation is conservative on that and says "no".
But I'm doing this anyway in one of my linux programs and it seems to work fine. Digging into boost/asio/detail/epoll_reactor.ipp shows that the function start_op() is indeed protected by a mutex, so my confidence is increased.
But other platforms may be different..
来源:https://stackoverflow.com/questions/7639349/boost-asio-thread-safety