gpt4 book ai didi

windows - 什么是装载机锁?

转载 作者:可可西里 更新时间:2023-11-01 14:45:13 27 4
gpt4 key购买 nike

我正在处理线程并且有一个潜在的死锁问题。有人向我提到装载机锁。

我在网上找不到太多信息。有人可以帮我解释一下“什么是加载器锁”吗?

最佳答案

例如,复习这个问题:

Loader lock error

The general idea of loader lock: The system runs the code in DllMain inside a lock (as in - synchronization lock). Therefore, running non-trivial code inside DllMain is "asking for a deadlock"

我提到的答案是基于这篇文章:

Another reason not to do anything scary in your DllMain: Inadvertent deadlock

Your DllMain function runs inside the loader lock, one of the few times the OS lets you run code while one of its internal locks is held. This means that you must be extra careful not to violate a lock hierarchy in your DllMain; otherwise, you are asking for a deadlock.

The loader lock is taken by any function that needs to access the list of DLLs loaded into the process. This includes functions like GetModuleHandle and GetModuleFileName. If your DllMain enters a critical section or waits on a synchronization object, and that critical section or synchronization object is owned by some code that is in turn waiting for the loader lock, you just created a deadlock:

// global variable
CRITICAL_SECTION g_csGlobal;

// some code somewhere
EnterCriticalSection(&g_csGlobal);
... GetModuleFileName(MyInstance, ..);
LeaveCriticalSection(&g_csGlobal);

BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason) {
...
case DLL_THREAD_DETACH:
EnterCriticalSection(&g_csGlobal);
...
}
...
}

请阅读整篇文章以充分理解。

关于windows - 什么是装载机锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13874324/

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