gpt4 book ai didi

c++ - 无法在 DLL 上锁定 C++ 11 std::mutex

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:53:14 25 4
gpt4 key购买 nike

<分区>

我试图通过使用 std::lock 对象来防止对 DLL 初始化函数的多次调用。

在独立程序上使用这样的程序时:

#include <mutex>
std::mutex mtx;

void main(){
mtx.lock();
(...)
mtx.unlock()
}

在 DLL 上调用时,这段完全相同的代码无法通过 mtx.lock()

BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
f_Init(VERSION_ID);
return TRUE;
}

//On another cpp file, a static library actually.
#include <mutex>
std::mutex mutex_state;
void f_Init(DWORD version){
//Acquire the state lock
mutex_state.lock(); //<-- Will NOT get past this line
(...)
mutex_state.unlock();
}

为什么在这种情况下无法锁定互斥体?

我目前使用的是 Microsoft Visual Studio 2013。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com