设置 OpenSSL 回调以使其线程安全很容易,所以为什么不这样做,看看问题是否消失。
虽然您已经推断没有必要这样做,但存在潜在问题的事实不应该让您对潜在的简单解决问题视而不见,即使“不可能那样”......
这是我用于此的代码,
struct CRYPTO_dynlock_value
{
CRYPTO_dynlock_value()
{
::InitializeCriticalSection(&crit);
}
~CRYPTO_dynlock_value()
{
::DeleteCriticalSection(&crit);
}
CRITICAL_SECTION crit;
};
static CRITICAL_SECTION *InitStaticCrit()
{
CRITICAL_SECTION *pCrit = new CRITICAL_SECTION();
::InitializeCriticalSection(pCrit);
return pCrit;
}
static CRITICAL_SECTION *s_pCriticalSection = InitStaticCrit();
static CRITICAL_SECTION *s_pLocks = 0;
static struct CRYPTO_dynlock_value *dyn_create_function(
const char *file,
int line)
{
(void)file;
(void)line;
CRYPTO_dynlock_value *pValue = new CRYPTO_dynlock_value();
return pValue;
}
static void dyn_lock_function(
int mode,
struct CRYPTO_dynlock_value *pLock,
const char *file,
int line)
{
(void)file;
(void)line;
if (mode & CRYPTO_LOCK)
{
::EnterCriticalSection(&pLock->crit);
}
else
{
::LeaveCriticalSection(&pLock->crit);
}
}
static void dyn_destroy_function(
struct CRYPTO_dynlock_value *pLock,
const char *file,
int line)
{
(void)file;
(void)line;
delete pLock;
}
static bool ThreadingSetup(
const DWORD spinCount)
{
::EnterCriticalSection(&s_criticalSection);
bool ok = false;
if (!s_pLocks)
{
s_pLocks = (CRITICAL_SECTION *)malloc(CRYPTO_num_locks() * sizeof(CRITICAL_SECTION));
for (int i = 0; i < CRYPTO_num_locks(); i++)
{
if (spinCount != 0)
{
#if(_WIN32_WINNT >= 0x0403)
(void)::InitializeCriticalSectionAndSpinCount(&s_pLocks[i], spinCount);
#else
#pragma warning(suppress: 6011) // Dereferencing null pointer. No, we're not.
::InitializeCriticalSection(&s_pLocks[i]);
OutputDebugString(_T("CUsesOpenSSL::ThreadingSetup() - spin count specified but _WIN32_WINNT < 0x0403, spin count not used\n"));
#endif
}
else
{
#pragma warning(suppress: 6011) // Dereferencing null pointer. No, we're not.
::InitializeCriticalSection(&s_pLocks[i]);
}
}
CRYPTO_set_locking_callback(LockingCallback);
//CRYPTO_set_id_callback(id_function);
// dynamic locks callbacks
CRYPTO_set_dynlock_create_callback(dyn_create_function);
CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
ok = true;
}
::LeaveCriticalSection(&s_criticalSection);
return ok;
}
static void ThreadingCleanup()
{
if (s_pLocks)
{
CRYPTO_set_locking_callback(0);
CRYPTO_set_dynlock_create_callback(0);
CRYPTO_set_dynlock_lock_callback(0);
CRYPTO_set_dynlock_destroy_callback(0);
for (int i = 0; i < CRYPTO_num_locks(); i++)
{
::DeleteCriticalSection(&s_pLocks[i]);
}
free(s_pLocks);
s_pLocks = 0;
}
}
static void LockingCallback(
int mode,
int type,
const char * /*file*/,
int /*line*/)
{
if (mode & CRYPTO_LOCK)
{
::EnterCriticalSection(&s_pLocks[type]);
}
else
{
::LeaveCriticalSection(&s_pLocks[type]);
}
}