DWORD __stdcall mythread(void* )
{
while( true )
{
char* p = new char[1024];
delete p;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h = CreateThread(NULL, 0, mythread, NULL, 0, NULL);
Sleep(1000);
TerminateThread(h, 0);
h = NULL;
char* p = new char[1024]; //这里会死锁,过不去
delete p;
return 0;
}
为什么死锁呢?new操作符用的是小块堆,整个进程在分配和回收内存时,都要用同一把锁。如果一个线程在占用该锁时被杀死(即临死前该线程在new或delete操作中),其他线程就无法再使用new或delete了,表现为hang住。
《核心编程》里明确提醒不要TerminateThread,但原因并不是血淋淋滴。今天发现的这个bug印证了此书的价值。
另注:许多临时的网络操作经常用TerminateThread,作为网络不通时的退出机制,以后要改改了。比如让该线程自生自灭,自行退出。
可能引发的问题包括: 1 If the target thread owns a critical section, the critical section will not be released. (未释放互斥区,造成死锁) 2 If the target thread is allocating memory from the heap, the heap lock will not be released. (未释放堆分配锁,造成死锁) 3 If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. (在执行内核函数时退出,造成该线程所在进程状态不确定,程序可能崩溃) 4 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得程序可能出现问题!)
分享到:
相关推荐
Why you should never call Suspend/TerminateThread
这个程序可以检查pop3邮件信息,纯粹的Winsock和Win32 API,不用 MFC以及Winsock2。所使用的API函数用到了CreateThread,GetOpenFileName,trayicons,SetTimer,sndPlaySound,GetExitCodeThread,TerminateThread...
虽然微软提供了 SuspendThread、TerminateThread 等函数“似乎”可以完成这个功能,但如果你在代码里使用这些函数,则往往会遇到各种问题。附件里是源码和Demo程序,并有详细的使用说明。(转) 亲测好用: 简单的说...
易语言取本程序线程源码,取本程序线程,子程序1,CreateToolhelp32Snapshot,GetWindowThreadProcessId,Thread32First,Thread32Next,CloseHandle,OpenThread,GetCurrentThreadId,TerminateThread
这个程序可以检查pop3邮件信息,纯粹的Winsock和Win32 API,不用 MFC以及Winsock2。所使用的API函数用到了CreateThread,GetOpenFileName,trayicons,SetTimer,sndPlaySound,GetExitCodeThread,TerminateThread...
IsDone变量的引入,是为了避免控制台应用的主线程执行结束时,进程中止导致新创建线程中止。——这甚至可能发生在新线程执行writeln()之前。 在线程函数中,随时可以调用ExitThread()来退出,或者当代码执行到...
虽然微软提供了 SuspendThread、TerminateThread 等函数“似乎”可以完成这个功能,但如果你在代码里使用这些函数,则往往会遇到各种问题(比如SuspendThread时正好在CRT分配内存的锁中造成其他线程也跟着锁死,...
C++11关于thead的应用,利用std::condition std::mutex提供如何中断,停止和继续功能,
3、同一个进程或另一个进程中的线程调用TerminateThread函数(应避免使用该方法)。 4、ExitProcess和TerminateProcess函数也可以用来终止线程的运行(应避免使用该方法)。 下面将详细介绍终止线程运行的方法:1-4...
易语言挂起进程源码,挂起进程,CreateProcess,CreateThread,SuspendThread,ResumeThread,TerminateThread
这是微软的一个关于线程的例子,演示如何杀死一个线程。而不用 TerminateThread
易语言控制台时钟源码,控制台时钟,子程序1,Sleep,循环事件_,消息循环_,暂停_,停止_,创建时钟_,运行_,TranslateMessage,GetMessage,DispatchMessage,CreateThread,ResumeThread,SuspendThread,TerminateThread,Module...
注意的是如果线程的终结是使用TerminateThread来完成的,那么系统将不会使用值DLL_THREAD_DETACH来执行线程的清理工作,这也就是说可能会造成数据丢失,所以不要使用TerminateThread来终结线程。以上所有讲解在工程...
易语言简单虚拟按键源码,简单虚拟按键,EditWndProc,FakeThread,SetWindowLong,CallWindowProc,CreateThread,TerminateThread,keybd_event,MessageBox
if(TerminateThread(hThread1,0))//前些终止线程 { CloseHandle(hThread1);//销毁线程句柄 } else { MessageBox("终止进程失败!"); } 11. 为应用程序添加WM_TIMER消息,实时更新全局变量的值到编辑框;
TerminateThread(m_hThread, 0x03); CloseHandle(m_hThread);//线程对象还在内存中, 要释放 m_bProcessing=false; //m_Animation.ShowWindow(SW_HIDE); //m_Animation.Stop(); } catch(...) { ...
再用TerminateProcess关闭,如果失败就枚举该进程中的线程并用TerminateThread关闭。然后再用TerminateProcess结束进程。这样就基本上可以关闭99%的非系统进程了。 还有,对于被注入了病毒dll的进程,要先枚举进程...
易语言类回调源码,类回调,Thread_echo_var,Create,Close,SetVar,SetSpeed,CreateThread,CloseHandle,TerminateThread