【发布时间】:2015-03-02 20:40:06
【问题描述】:
我有多个 SSL 服务器在不同的线程中运行,我无法在 openssl c++ 中定义锁定方法?我应该为整个应用程序或多个(即每个线程)定义一种锁定/清理方法吗?
【问题讨论】:
标签: c++ multithreading openssl
我有多个 SSL 服务器在不同的线程中运行,我无法在 openssl c++ 中定义锁定方法?我应该为整个应用程序或多个(即每个线程)定义一种锁定/清理方法吗?
【问题讨论】:
标签: c++ multithreading openssl
在主应用程序线程中初始化库一次,就像这里:https://github.com/openssl/openssl/blob/master/crypto/threads/mttest.c#L331 或这里的示例中的相同内容:https://github.com/openssl/openssl/tree/master/crypto/threads
【讨论】:
我应该为整个应用程序定义一个锁定/清理方法还是多个,即每个线程?
OpenSSL不使用线程本地存储。所以你应该不尝试在每个线程的基础上安装锁。
您应该在应用程序范围内提供锁。
这是相关来源。
$ grep -R CRYPTO_thread_setup *
crypto/threads/th-lock.c: * CRYPTO_thread_setup();
crypto/threads/th-lock.c:void CRYPTO_thread_setup(void)
...
下面是一个设置示例(其中有一些适用于不同的平台):
static HANDLE *lock_cs;
...
void CRYPTO_thread_setup(void)
{
int i;
lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
for (i=0; i<CRYPTO_num_locks(); i++)
{
lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
}
CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
/* id callback defined */
return(1);
}
请注意,在整个应用程序中使用了一组锁。
【讨论】: