运行此代码,a2线程会卡死,因为在LoadLibrary过程中会有一个临界区被占用,如果正好是这个时候此线程被TerminateThread终止,那么所有的loadlibrary及其他使用此锁的线程都会进入一个无限等待这个锁释放的状态。所以不光是自己实现的锁要小心,系统某些API也要小心,因为其内部也有同步的逻辑。

原因是(引自MSDN):

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:

  • If the target thread owns a critical section, the critical section will not be released.
  • If the target thread is allocating memory from the heap, the heap lock will not be released.
  • If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
  • If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.

#include <windows.h>
#include <stdio.h>

DWORD WINAPI a1(LPVOID lp)
{
    do 
    {
        HMODULE h = LoadLibraryExW(L"Wtsapi32.dll",0,0);
        Sleep(1);
        FreeLibrary(h);
    } while (1);
    return 0;
}

DWORD WINAPI a2(LPVOID lp)
{
    int i = 0;
    do
    {
        HMODULE h = LoadLibraryExW(L"Wtsapi32.dll",0,0);
        Sleep(1);
        FreeLibrary(h);

printf("\r%d", i++);
    } while (1);
    return 0;
}

DWORD WINAPI a3(LPVOID lp)
{
    do
    {
        HANDLE h = CreateThread(0, 0, a1, 0, 0, 0);
        Sleep(200);
        TerminateThread(h,0);
    } while (1);
    return 0;
}

int main(int argc ,char *argv[])
{
    CloseHandle(CreateThread(0, 0, a3, 0, 0, 0));
    CloseHandle(CreateThread(0, 0, a2, 0, 0, 0));

do 
    {
        Sleep(10000);
    } while (1);
    return 0;
}

TerminateThread 导致LoadLibary 死锁相关推荐

  1. [原]调试TerminateThread导致的死锁

    这是继上一篇 [原]调试DLL卸载时的死锁 后的又一篇使用windbg调试死锁的文章.希望能对大家有所帮助. 前言 之前项目里的一个升级程序偶尔会死锁,查看dump后发现是死在了ShellExecut ...

  2. terminatethread导致内存泄露

    terminatethread导致内存泄露,waitforsingleobject又容易导致UI卡死,真头疼 听过无数次不要TerminateThread,只是工作中常用,貌似也没有什么问题.今天在高 ...

  3. MySQL 5.6.35 索引优化导致的死锁案例解析

    一.背景 随着公司业务的发展,商品库存从商品中心独立出来成为一个独立的系统,承接主站商品库存校验.订单库存扣减.售后库存释放等业务.在上线之前我们对于核心接口进行了压测,压测过程中出现了 MySQL ...

  4. 由于不当的执行顺序导致的死锁

    为了保证线程的安全,我们引入了加锁机制,但是如果不加限制的使用加锁,就有可能会导致顺序死锁(Lock-Ordering Deadlock).上篇文章我们也提到了在线程词中因为资源的不足而导致的资源死锁 ...

  5. postgresql 并发访问_postgresql 并发update下导致的死锁问题

    一.死锁问题背景 在收据批量打印时,由于采用异步并发触发打印,同时触发打印(九千多数据 每隔50ms触发一次),导致了并发执行引起在接口更新打印次数时postgresql发生死锁问题, 具体报错如下: ...

  6. 导致线程死锁的原因?怎么解除线程死锁

    欢迎大家关注我的公众号[老周聊架构],Java后端主流技术栈的原理.源码分析.架构以及各种互联网高并发.高性能.高可用的解决方案. 一.导致线程死锁的原因 多个线程同时被阻塞,它们中的一个或者全部都在 ...

  7. 线上MySQL死锁分析——索引设置不当导致的死锁

    文章目录 1. 背景 2. MySQL InnoDB的锁机制 2.1 MySQL中的锁类型 2.2 行锁的加锁规则 2.3 死锁检测机制 3. 本文案例分析 3.1 分析InnoDB status日志 ...

  8. TerminateThread导致程序未响应

    TerminateThread函数的作用是在线程外终止一个线程,属于强制终止,线程里面的资源不会被回收,所以使用此函数是很危险的.比如下面的两个例子 demo1: DWORD __stdcall my ...

  9. hive insert into values 没反应_再遇死锁insert语句导致的死锁

    日前,我们生产上遇到了一个死锁现象,通过show engine innodb status 看到死锁信息如下(场景复现,生产上的语句不同,但情况完全相同): 如图所示,能看到的信息,仅仅看到两个相同& ...

最新文章

  1. 计算机电缆djyvp工艺,计算机电缆dJyvP相关办法.pdf
  2. 常见字符串和数值间得转换
  3. 杨剑勇:物联网是一个未来概念?其实就在身边
  4. 如何挑选适合的前端框架(去哪儿网前端架构师司徒正美)
  5. json loads No JSON object could be decoded 问题解决
  6. oracle之 安装 11G RAC 报 NTP failed
  7. 移动硬盘属性0字节找回数据的方案
  8. 7 centos 查看程序文件数量_CentOS之使用Systemd添加自定义系统服务
  9. Java PushbackReader ready()方法与示例
  10. 5g虚拟技术旅游_5G造就文旅新时代,驴迹科技探索智慧出行创新领域
  11. mac 本地安装mysql_mac 本地安装mysql服务
  12. hyperledger fabric PBFT算法简要解析
  13. 分享 Android 手机屏幕录制并制作成 GIF 演示图片
  14. JavaScript高级程序设计知识点汇总
  15. 基于Java的办公用品管理系统的设计与实现
  16. python3中单引号,双引号,三个单引号 ,三个双引号的差别,以及反斜杠的用法
  17. 由浪潮8控存储登顶SPC-1所想,存储系统一个长期被忽略的指标
  18. 计算机图表制作教程,PPT怎么制作动态图表 PPT动态图表制作教程-电脑教程
  19. 点线面的特点_夯实基础!点线面的基本特点与表现
  20. 【开关电源】matlab/simulink入门

热门文章

  1. 高可用架构之限流降级
  2. 常规密码学加解密脚本(python)
  3. 大数据之Flume:Flume概述
  4. python在水文领域中的应用_python在水利工程或者水文方向上有什么案例可以学习一下么?或者还要学什么库。?...
  5. Elmedia Player GO for Mac中文破解版永久激活教程
  6. hadoop集群安装ES(ElasticSearch 5.0.2)
  7. Camstar开发C#代码规范、Camstar更新以及Designer 开发规范
  8. 通信原理 简易2DPSK的相干解调与非相干解调的matlab实现
  9. Oracle ADG 与 DG 的区别
  10. 群晖DS918+ 二合一 分区扩充教程