多线程程序通常很容易使用TerminateThread函数。 此函数的功能是用于结束工作的线程。 在MSDN上,REMARKS说:
terminatethreadisusedtocauseathreadtoexit.whenthisoccurs, thetargetthreadhasnochancetoexecuteanyuser-mode code.dllsattachedtothethreadarenotnotifiedthatthethreadisterminating.thesthethreathreadination
windows server 2003 and windows XP/20003360 thetargetthread ' sinitial stackisnotfreed, causingaresourceleak.terminatethreadisadangerousfunctionthatshouldonlybeusedinthemostextremecases.youshouldcallterminateteterminateteteththh tthetargetthreadisdoing,andyoucontrolallofthecodethatthetargetthreadcouldpossiblyberunningatthetimeofthetermination.for exather
ifthetargetthreadownsacriticalsection,thecriticalsectionwillnotbereleased.ifthetargetthreadisallocatingmemoryfromtheheap, theheaplockwillnotbereleased.ifthetargetthreadisexecutingcertainkernel 32 callswhenitisterminated, the kernel32 stateforthethread ' sprocesscouldbeinconsistent.ifthetargetthreadismanipulatingtheglobalstateofashareddll,thesttate affectingotherusersofthedll.athreadcannotprotectitselfagainstterminatethread, otherthanbycontrollingaccesstoitshandles.thethreadhandlereturnedbythecreatethreadandcreateprocessfunctionshasthread _ termi
ifthetargetthreadisthelastthreadofaprocesswhenthisfunctioniscalled,thethread ' sprocessisalsoterminated。
thestateofthethreadobjectbecomessignaled,releasinganyotherthreadsthathadbeenwaitingforthethreadtoterminate.thethread ' steed
terminatingathreaddoesnotnecessarilyremovethethreadobjectfromthesystem.athreadobjectisdeletedwhenthelastthreadhandleisclosed
其中,如果目标线程进入临界区域,则TerminateThread会导致该线程无法释放。 这次项目中程序没有反应的问题是因为这个。
当线程运行malloc或free函数时,它将进入堆的临界区域。 此时,如果线程中断,其他线程将等待并僵硬。
此时进入程序调用堆栈,可以看到很多线程死亡的free和malloc的内部。
所以要结束线程,最好不要用这个函数中断。
转载于:https://www.cn blogs.com/Ming xx/archive/2011/06/25/2089954.html