10
我已經創建了這個「線程安全」通用屬性,可以在主線程和後臺線程之間使用。我做了它,因爲我厭倦了爲我所有的屬性和變量創建鎖定對象。通用線程安全屬性
TLockedProp<MyType> = class
private
FMyProp:MyType;
PropLock:TObject;
procedure SetMyProp(const Value: MyType);
function GetMyProp: MyType;
published
property Value:MyType read GetMyProp write SetMyProp;
public
Constructor Create;
Destructor Destroy;override;
end;
{ TLockedProp<MyType> }
constructor TLockedProp<MyType>.Create;
begin
inherited;
PropLock:=TObject.create
end;
destructor TLockedProp<MyType>.Destroy;
begin
PropLock.Free;
inherited;
end;
function TLockedProp<MyType>.GetMyProp: MyType;
begin
TMonitor.Enter(PropLock);
result := FMyProp;
TMonitor.Exit(PropLock);
end;
procedure TLockedProp<MyType>.SetMyProp(const Value: MyType);
begin
TMonitor.Enter(PropLock);
FMyProp := Value;
TMonitor.Exit(PropLock);
end;
有沒有我忽略的問題? 這是一些使用此屬性類的代碼。告訴我你的想法。
TBgThread=class(TThread)
private
FPaused: TLockedProp<boolean>;
FCount:TLockedProp<integer>;
procedure ChangeCount(pPlusMin:integer);
function GetPaused:boolean;
function GetCount:integer;
public
constructor Create;
destructor Destroy;override;
{Toggle Pause}
procedure PausePlay;
protected
procedure Execute;override;
published
Property Paused:boolean read GetPaused;
Property Count:integer read GetCount;
end;
constructor TBgThread.Create();
begin
inherited Create(true);;
FPaused:=TLockedProp<boolean>.create;
FPaused.Value:=false;
FCount:=TLockedProp<integer>.create;
FCount.Value:=0;
end;
destructor TBgThread.Destroy;
begin
FPaused.Free;
FCount.free;
inherited;
end;
procedure TBgThread.Execute;
begin
inherited;
Repeat
if not Paused then begin
Try
//do something
finally
ChangeCount(+1);
end;
end else
Sleep(90);
Until Terminated;
end;
function TBgThread.GetCount: integer;
begin
Result:=FCount.Value;
end;
procedure TBgThread.ChangeCount(pPlusMin: integer);
begin
FCount.Value:=FCount.Value+pPlusMin;
end;
function TBgThread.GetPaused: boolean;
begin
result := FPaused.Value;
end;
procedure TBgThread.PausePlay;
begin
FPaused.Value:=not FPaused.Value;
end;
Thnx爲你的答案,我真的很感謝你分享你的課堂版本。作爲一名程序員,讓我感到更加自信,因爲我能夠自己想出這個解決方案。只有一年的德爾福在我的腰帶下;)。 –
請注意,如果保存其緩衝區的類比CPU緩存線更小,則關鍵部分可能會出現性能問題。請參見https://www.delphitools.info/2011/11/30/fixing-tcriticalsection/向類定義中添加小型對齊緩衝區可能會更好/更安全,並且依賴於OS臨界區而不是TCriticalSection類。 –