線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5597
推到 Plurk!
推到 Facebook!

如何將 thread 終止?

尚未結案
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-05-17 13:21:01 IP:61.57.xxx.xxx 未訂閱
欲由primary thread將新產生的thread終止的方法,似乎只有TerminateThread這個函式可以做到,不過我查書之後發現以下的問題: 當thread被ExitThread結束,其堆疊會被系統釋放。但如thread是被TerminateThread結束,因為有可能其他thread仍在使用它的資料(透過指標),堆疊會被保留到整個處理程序的結束。當thread正常結束時,系統會通知擁有thread的處理程序所用的每個DLL,但如呼叫TerminateThread,系統就不會這樣通知,這可能會造成DLL無法正常的關閉,例如,thread釋放DLL時,DLL可能要將記憶體的資料寫入磁碟,當以TerminateThread結束thread時DLL將無法進行正常的結束動作。    這樣是不是意謂呼叫TerminateThread這個函式,感覺上像是突然的把thread終止掉,而沒有做後續的處理動作,這樣會造成什麼問題呢?    還有看到有CloseHandle(hThread),這樣的用法?不過似乎沒有任何作用? 不知道要如何才能正常的終止一個thread?謝謝!   
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-05-18 08:26:07 IP:211.23.xxx.xxx 未訂閱
CloseHandle(hThread)已經可以正常結束一個thread.    TerminateThread應該注意的事項以及會出現的問題,msdn上已經有警告與說明, 節錄如下:    TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating. 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. 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. If the target thread is the last thread of a process when this function is called, the thread's process is also terminated. The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed. ------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖VS東方不敗:D ------------------------------- 發表人 - anpino 於 2004/05/18 08:49:55
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-05-18 08:57:21 IP:211.23.xxx.xxx 未訂閱
hornacekさん, 阿咧結案也太快了吧? (我的回答還沒寫完整咧...) < > 希望上面那篇回覆可以讓您有所收穫。 < > ------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖VS東方不敗:D -------------------------------
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-05-18 09:13:43 IP:61.57.xxx.xxx 未訂閱
那這樣說起來,TerminateThread是一個相當危險的函數,除非我很清楚我在做什麼?那我還有什麼其它的方式可以做到隨時終止一個函數的執行呢?到目前為止,我只能想到用thread的方式執行函數,再用TerminateThread來隨時終止它。剛剛查閱了一下MSDN,對於CloseHandle的解釋: The CloseHandle function closes an open object handle.     Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.    因些要終止thread,我使用的方式如下:    TerminateThread(hThread, 0); CloseHandle(hThread);    其中hThread是CreateThread所產生的HANDLE值。    Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.    這一句話,是不是代表如果我增加一個thread的物件,最後如果這個thread被終止後,就會自動被刪除,而我還要加上CloseHandle來關閉它嗎?    至於ExitThread,似乎是個無用的東西...因為它只能寫在這個thread的執行函式裡,可是thread本來執行到最後就會terminate掉,真不知道ExitThread有什麼用?    咳~~~欲達到隨時終止一個函式的執行,怎麼這麼麻煩~~~? 感謝anpino版主一再熱心的指導...  發表人 -
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-05-18 10:08:01 IP:211.23.xxx.xxx 未訂閱
1. 關於CloseHandle與thread的互動: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_threads.asp    2. TerminateThread(hThread, 0); CloseHandle(hThread);    這樣做是可以的,但是必須先閃過TerminateThread會造成的問題, 也就是msdn上對於TerminateThread要程式設計者注意的那4點。    3. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.    這一句話是說, 終止一個thread並沒有必要從系統移除這個thread 物件, 因為 thread 物件會在系統結束時被移除/釋放。(也就是process會清除自己程式內產生的任何物件與記憶體。)    4.
引言: 至於ExitThread,似乎是個無用的東西...因為它只能寫在這個thread的執行函式裡,可是thread本來執行到最後就會terminate掉,真不知道ExitThread有什麼用?
舉個例子, 一個funtion 也有可能在程式中途return 不是嗎? 不見得每次call function時, 都要function執行到最後呀~~ "return相對於function"與"ExitThread相對於thread"的意思是一樣的。 ------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖VS東方不敗:D -------------------------------
系統時間:2024-05-18 14:31:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!