簡單說說Delphi中線程的釋放

2021-08-07 22:29:01 字數 2654 閱讀 2096

標籤:delphi

constructor

2011-08-26 14:53 9202人閱讀

(0) 

收藏舉報

分類:delphi程式設計

(25)

執行緒的釋放方式有兩種:一種是執行緒在執行完成後自動釋放,一種是手動釋放。

然而執行緒的停止也有兩種情況:一種是不需要設定標誌位,直接完成;一種是由於execute方法中做了迴圈,需要設定標誌位才能停止。

如果執行緒已經停止並且自動釋放,再去手動停止,就會報錯。

下面看**:

1、自動停止後自動釋放的執行緒:

[delphi]

view plain

copy

1.constructorttestthread.create;  

2.begin

3.inheritedcreate( true );  

4.   freeonterminate := true;  

5.end;  

6.   

7.procedurettestthread.execute;  

8.begin

9.     ....//功能**

10.   

11.     //此方法完成後執行緒就已經停止了

12.   

13.end;  

這種情況執行緒會自動釋放,因此不要手動釋放,否則會報錯

2、手動停止後自動釋放的執行緒:

[delphi]

view plain

copy

1.constructorttestthread.create;  

2.begin

3.inheritedcreate( true );  

4.   freeonterminate := true;  

5.end;  

6.   

7.procedurettestthread.execute;  

8.begin

9.while

notterminateddo//not terminated do

10.begin

11.     ....//功能**

12.end;  

13.end;  

14.   

15.proceduretest  

16.begin

17.     t1 := ttestthread.create( self );  

18.     t1.terminate;  

19.end;  

3、手動釋放的執行緒:

[delphi]

view plain

copy

1.constructorttestthread.create;  

2.begin

3.inheritedcreate( true );  

4.end;  

5.   

6.procedurettestthread.execute;  

7.begin

8.while

notterminateddo//not terminated do

9.begin

10.     ....//功能**

11.end;  

12.end;  

13.   

14.proceduretest  

15.begin

16.     t1 := ttestthread.create( self );  

17.     t1.terminate;  

18.     t1.waitfor;  

19.     t1.free;  

20.end;  

那麼,何時使用自動釋放的執行緒,何時使用手動釋放的執行緒呢

我的建議是:

如果這個執行緒執行時間很短或者能保證在系統退出前完成執行,則可以選擇自動釋放,因為它可以很快自動釋放掉

如果這個執行緒執行貫穿系統執行整個期間,則要選擇手動釋放了

簡單說說Delphi中線程的釋放

執行緒的釋放方式有兩種 一種是執行緒在執行完成後自動釋放,一種是手動釋放。然而執行緒的停止也有兩種情況 一種是不需要設定標誌位,直接完成 一種是由於execute方法中做了迴圈,需要設定標誌位才能停止。如果執行緒已經停止並且自動釋放,再去手動停止,就會報錯。下面看 1 自動停止後自動釋放的執行緒 c...

簡單說說android的執行緒封裝

對執行緒的c 封裝 其實api已經寫得很清楚了 封裝的檔案 frameworks base include utils threads.h 這裡不討論具體實現,具體實現是和系統相關聯的 首先anroid提供了幾個與直接建立執行緒的函式 inline bool createthread thread ...

簡單說說U boot的修改

sdram 32mbytes ncs1 flash 8mbytes ncs0 涉及到的檔案有四個 common.h flash.c flash.h board at91rm9200dk config.mk 以下簡單的說說。一 首先讀讀uboot自帶的readme檔案,了解了乙個大概。二 看看comm...