gpt4 book ai didi

multithreading - Delphi2006-是否存在带有TMultiReadExclusiveWriteSynchronizer的TList?

转载 作者:行者123 更新时间:2023-12-03 18:37:35 26 4
gpt4 key购买 nike

我有多线程的外部应用程序,并且此应用程序正在使用我的自定义dll从该线程执行某些操作。
在这个dll中,我有2个函数可以读取和向TList写入一些数据。
我需要这些线程自由读取该列表,但一次只能写入一个列表,其余列表必须等待它们的写入时间。

我的问题:
-BDS 2006中是否有具有TMREWSync功能的TList组件,或
-也许您知道我可以在我的应用中使用的任何免费第三方组件,或者
-也许您有一些自定义的TList代码,可以完成上述操作。

编辑:
我需要类似TThreadList.LockList的东西,但仅用于写入该列表。

谢谢你的帮助。

最佳答案

以与TMultiReadExclusiveWriteSynchronizer相同的方式将TListTThreadList放在一起很简单。如果您已经知道这些类的工作原理,则可以遵循以下代码。

type
TReadOnlyList = class
private
FList: TList;
function GetCount: Integer;
function GetItem(Index: Integer): Pointer;
public
constructor Create(List: TList);
property Count: Integer read GetCount;
property Items[Index: Integer]: Pointer read GetItem;
end;

TMREWList = class
private
FList: TList;
FReadOnlyList: TReadOnlyList;
FLock: TMultiReadExclusiveWriteSynchronizer;
public
constructor Create;
destructor Destroy; override;
function LockListWrite: TList;
procedure UnlockListWrite;
function LockListRead: TReadOnlyList;
procedure UnlockListRead;
end;

{ TReadOnlyList }

constructor TReadOnlyList.Create(List: TList);
begin
inherited Create;
FList := List;
end;

function TReadOnlyList.GetCount: Integer;
begin
Result := FList.Count;
end;

function TReadOnlyList.GetItem(Index: Integer): Pointer;
begin
Result := FList[Index];
end;

{ TMREWList }

constructor TMREWList.Create;
begin
inherited;
FList := TList.Create;
FReadOnlyList := TReadOnlyList.Create(FList);
FLock := TMultiReadExclusiveWriteSynchronizer.Create;
end;

destructor TMREWList.Destroy;
begin
FLock.Free;
FReadOnlyList.Free;
FList.Free;
inherited;
end;

function TMREWList.LockListWrite: TList;
begin
FLock.BeginWrite;
Result := FList;
end;

procedure TMREWList.UnlockListWrite;
begin
FLock.EndWrite;
end;

function TMREWList.LockListRead: TReadOnlyList;
begin
FLock.BeginRead;
Result := FReadOnlyList;
end;

procedure TMREWList.UnlockListRead;
begin
FLock.EndRead;
end;

这是最基本的实现。如果您愿意,可以按照 TThreadList的方式添加一些其他功能。

关于multithreading - Delphi2006-是否存在带有TMultiReadExclusiveWriteSynchronizer的TList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com