什么是装载机锁? [英] what is a loader lock?

查看:56
本文介绍了什么是装载机锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理线程,并且有潜在的死锁问题.有人向我提到了装载机锁.

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" ?

推荐答案

例如,查看以下问题:

加载程序锁定错误

加载程序锁的一般概念:系统在锁中(如-同步锁中)在DllMain中运行代码.因此,在DllMain内部运行非平凡的代码就是寻求死锁"

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"

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

Answer I've mentioned is based on this article:

在您的DllMain中不做任何令人恐惧的另一个原因:无心死锁

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

您的DllMain函数在加载程序锁内部运行,这是操作系统在保持其内部锁之一的同时让您运行代码的几次机会之一.这意味着您必须格外小心,不要违反您的DllMain中的锁层次结构.否则,您会陷入僵局.

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.

需要访问加载到进程中的DLL列表的任何函数都使用了加载器锁.这包括GetModuleHandleGetModuleFileName之类的功能.如果您的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.

这篇关于什么是装载机锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆