我知道 OpenSSL,boost asio SSL 实现基于,不允许并发 SSL_read() 和 SSL_write()(即由不同线程执行的 SSL_read() 和 SSL_write())。
从同一线程在 SSL 套接字上调用 boost asio async_read() 和 async_write() 是否安全?
谢谢
最佳答案
boost::asio::ssl:::stream
的要求是为了线程安全;它不要求哪个线程可以启动操作:
Distinct objects: Safe.
Shared objects: Unsafe. The application must also ensure that all asynchronous operations are performed within the same implicit or explicit strand.
如果应用程序只有一个线程处理 io_service
,并且 async_read()
和 async_write()
是从该线程内启动的,那么它是安全的,因为操作和完成处理程序在隐式链中运行。
另一方面,如果多个线程正在处理 io_service
,则需要显式的 strand
。 async_read()
和 async_write()
操作需要从 strand
中启动,并且完成处理程序需要由相同的包装链
。
有关 Boost.Asio 的线程安全要求、strands
和组合操作的更多详细信息,请考虑阅读 this回答。
关于c++ - boost asio - 来自一个线程的 SSL async_read 和 async_write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409517/