簡單說說Delphi中線程的釋放

2021-05-27 17:28:40 字數 1471 閱讀 9873

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

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

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

下面看**:

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

constructor ttestthread.create;

begin

inherited create( true );

freeonterminate := true;

end;

procedure ttestthread.execute;

begin

....//功能**

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

end;

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

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

constructor ttestthread.create;

begin

inherited create( true );

freeonterminate := true;

end;

procedure ttestthread.execute;

begin

while not terminated do //not terminated do

begin

....//功能**

end;

end;

procedure test

begin

t1 := ttestthread.create( self );

t1.terminate;

end;

3、手動釋放的執行緒:
constructor ttestthread.create;

begin

inherited create( true );

end;

procedure ttestthread.execute;

begin

while not terminated do //not terminated do

begin

....//功能**

end;

end;

procedure test

begin

t1 := ttestthread.create( self );

t1.terminate;

t1.waitfor;

t1.free;

end;

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

我的建議是:

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

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

簡單說說Delphi中線程的釋放

標籤 delphi constructor 2011 08 26 14 53 9202人閱讀 0 收藏舉報 分類 delphi程式設計 25 執行緒的釋放方式有兩種 一種是執行緒在執行完成後自動釋放,一種是手動釋放。然而執行緒的停止也有兩種情況 一種是不需要設定標誌位,直接完成 一種是由於execu...

簡單說說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...