TerminateThread危险

TerminateThread危险TerminateThr nbsp isadangerous Youshouldcal nbsp TerminateThr nbsp onlyifyoukno andyoucontro

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.(在使用DLL时退出,造成DLL被销毁,其他使用该DLL得程序可能出现问题!)

A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.

 

 

 

听过无数次不要TerminateThread,只是工作中常用,貌似也没有什么问题。今天在高强度测试中发现了一个不可原谅的错误。参看下面的例子

 

《核心编程》里明确提醒不要TerminateThread,但原因并不是血淋淋滴。今天发现的这个bug印证了此书的价值。

另注:许多临时的网络操作经常用TerminateThread,作为网络不通时的退出机制,以后要改改了。比如让该线程自生自灭,自行退出。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205807.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午5:08
下一篇 2026年3月19日 下午5:08


相关推荐

  • Oracle数据库学习

    Oracle数据库学习Oracle 学习视频 一 oracle 数据库了解 1 Oracle 目录 admin cfgtoollogs checkpoints diag flash recovery area 闪回区

    2026年3月26日
    2
  • OpenGL 安装

    OpenGL 安装OpenGL 是一套由 SGI 公司发展出来的绘图函数库 它是一组 C 语言的函数 用于 2D 与 3D 图形应用程序的开发上 OpenGL 让程序开发人员不需要考虑到各种显示卡底层运作是否相同的问题 硬件由 OpenGL 核心去沟通 因此只要显示卡支援 OpenGL 那么程序就不需要重新再移植 而程序开发人员也不需要重新学习一组函数库来移植程序 安装首先不可或缺的就是编译器与基本的函数库

    2026年3月18日
    2
  • SPSS多元线性回归输出结果的详细解释

    SPSS多元线性回归输出结果的详细解释先说一句题外话 如果当年在大学里数理统计等课程结合 SPSS SAS R 等软件来讲 应该效果会好很多 最近做了一些用 SPSS 进行线性回归的实验 还是感觉很多细节把握不好 这里结合我的实验结果 以及网上别人的介绍总结一下 先贴几张 SPSS 的输出 下面简单解释一下这三张图中的结果 第一个表模型汇总表中 R 表示拟合优度 goodnessoffi 它是用来衡量估计的模型对观测

    2026年3月18日
    1
  • 立即收藏!闪剪Nano Banana最全变现教程

    立即收藏!闪剪Nano Banana最全变现教程

    2026年3月15日
    2
  • 电容材料分类_电容有什么材料

    电容材料分类_电容有什么材料转自:https://blog.csdn.net/qq_29350001/article/details/51142105?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.n

    2022年8月22日
    11
  • 写字机器人程序

    写字机器人程序#include”mainwindow.h”#include”ui_mainwindow.h”#include#include#include#include”math.h”#include#include#include#include//引入头文件/*定义常量*/#defineDIS_SMALL278;//定义小臂长度单位cm

    2022年7月16日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号