加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 服务器 > 系统 > 正文

windows – 什么是装载机锁?

发布时间:2021-02-01 11:05:03 所属栏目:系统 来源:网络整理
导读:我正在处理线程并且存在潜在的死锁问题.有人向我提到装载机锁. 我在网上找不到太多信息.有人可以帮我解释一下,“什么是Loader Lock”? 解决方法 例如,请查看此问题: Loader lock error The general idea of loader lock: The system runs the code in DllM

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

我在网上找不到太多信息.有人可以帮我解释一下,“什么是Loader Lock”?

解决方法

例如,请查看此问题:

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);
   ...
  }
  ...
}

请查看整篇文章以获得完整的理解.

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读