简体   繁体   English

OpenSSL-多线程环境-C ++

[英]OpenSSL - Multithread environment - C++

I'm using non-blocking sockets and OpenSSL 1.1.0 to create a simple web server. 我正在使用非阻塞套接字和OpenSSL 1.1.0创建一个简单的Web服务器。

The clients requests is handled inside 4 threads and the threads is created using C++ ( std::thread ). 客户端请求在4个线程内处理,并且使用C ++( std::thread )创建std::thread

My question is: Can we use the C++ threads to make OpenSSL thread safe for each client request? 我的问题是:我们可以使用C ++线程使每个客户端请求的OpenSSL线程安全吗?

I ask this because i have found this: 我问这是因为我发现了这一点:

*) OpenSSL now uses a new threading API. *)OpenSSL现在使用新的线程API。 It is no longer necessary to set locking callbacks to use OpenSSL in a multi-threaded environment. 不再需要设置锁定回调以在多线程环境中使用OpenSSL。 There are two supported threading models: pthreads and windows threads . 有两种受支持的线程模型: pthreadsWindowsthreads It is also possible to configure OpenSSL at compile time for "no-threads". 也可以在编译时为“无线程”配置OpenSSL。 The old threading API should no longer be used. 不再使用旧的线程API。 The functions have been replaced with "no-op" compatibility macros. 这些功能已被“无操作”兼容性宏替换。 [Alessandro Ghedini, Matt Caswell] [亚历山德罗·格迪尼,马特·卡斯韦尔]

简而言之, 默认情况下, openssl 不是线程安全的 ,但是您可以使用一些带有锁(std :: lock_guard)的互斥锁(std :: lock_guard)或原子数据类型std :: atomic的锁机制,必须先设置一些回调,但是现在,我现在不记得了,请检查此锁定机制示例https://github.com/openssl/openssl/blob/OpenSSL_1_0_2-stable/crypto/threads/th-lock.c并阅读此文章,其中包含一些内容例如https://www.openssl.org/blog/blog/2017/02/21/threads/您可能会参与其中,成瘾我认为std :: thread在后台使用pthread,但它可以使用本机(取决于OS )线程机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM