问题描述
我正在处理线程,并且有潜在的死锁问题.有人向我提到了装载机锁.
I was dealing with threads and have a potential deadlock problem. Someone mentioned to me about a loader lock.
我在网上找不到太多信息.有人可以帮我解释一下什么是 Loader Lock "吗?
I couldn't find much information online. Can someone please help me and explain, "What is a Loader Lock" ?
推荐答案
例如,查看以下问题:
我提到的答案是基于这篇文章的:
Answer I've mentioned is based on this article:
在您的DllMain
中不做任何令人恐惧的另一个原因:无心死锁
Another reason not to do anything scary in your DllMain
: Inadvertent deadlock
需要访问加载到进程中的DLL列表的任何函数都使用了加载器锁.这包括GetModuleHandle
和GetModuleFileName
之类的功能.如果您的DllMain
输入一个关键部分或等待同步对象,并且该关键部分或同步对象归于某些代码,这些代码又等待加载程序锁,那么您就创建了一个死锁:
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);
...
}
...
}
请阅读整篇文章以得到全面的理解.
Please review the whole article for full understanding.
这篇关于什么是装载机锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!