Boost.Asio thread safety

回眸只為那壹抹淺笑 提交于 2019-12-04 01:42:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!