Multithreading 多线程访问数据
我有一个我写的应用程序,我有三个线程。螺纹1、2和3 和两个数字1和2的列表 1号螺纹将数据插入1号列表 线程2从列表1获取数据 2号螺纹将数据插入2号列表 线程3从列表2获取数据 如果线程同时运行,则会占用大量CPU。可以在列表中输入数据,不需要2号和3号运行线程 如何通知新数据列表中的线程已插入,并且它们需要开始处理新的数据 联系您的技巧和有效方式Multithreading 多线程访问数据,multithreading,delphi,delphi-xe3,Multithreading,Delphi,Delphi Xe3,我有一个我写的应用程序,我有三个线程。螺纹1、2和3 和两个数字1和2的列表 1号螺纹将数据插入1号列表 线程2从列表1获取数据 2号螺纹将数据插入2号列表 线程3从列表2获取数据 如果线程同时运行,则会占用大量CPU。可以在列表中输入数据,不需要2号和3号运行线程 如何通知新数据列表中的线程已插入,并且它们需要开始处理新的数据 联系您的技巧和有效方式 非常感谢。首先想到的是信号量。信号量是一个同步原语,里面有一个计数器:一个线程尝试减少计数器,如果计数器为零,线程将阻塞(即,没有调度,不占用C
非常感谢。首先想到的是信号量。信号量是一个同步原语,里面有一个计数器:一个线程尝试减少计数器,如果计数器为零,线程将阻塞(即,没有调度,不占用CPU,只是无害地等待)。另一个线程增加计数器,导致阻塞的线程继续 每个列表可能都有一个信号量,因此消费线程在读取前会减少该信号量,而生产者线程在写入后会增加该信号量
还有一件事需要注意:是否允许一个线程在另一个线程修改列表时从列表中获取数据?这取决于列表(或集合)是如何实现的,所以请尝试在文档中找到关于列表“线程安全”的内容。也许你需要另一个同步原语:互斥。互斥锁由线程“获取”然后“释放”,当多个线程试图锁定互斥锁时,其中一个线程必须等待。每个列表中有一个互斥体,在保持该互斥体的情况下进行修改和读取,可能适合这里。下面是一个示例,它将三个线程中的值添加到列表1和列表2中 每次将值放入列表时,都会触发
事件
,处理此事件的线程将拉出列表中的最后一个值并清除事件标志
在清除事件标志之前,不能将新值放入列表中
中间线程对新值进行中间存储,而不是暂停第一个线程
所有事件都是可等待的,因此cpu可以轻松处理
这些列表是线程安全的
program Project62;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
System.SyncObjs,
System.Generics.Collections;
Type
TMyThread1 = Class(TThread)
private
fMySyncAddList : TSimpleEvent;
fMyList : TThreadList<Integer>;
fAddVal : Integer;
public
Constructor Create(ASyncAddList: TSimpleEvent; AList: TThreadList<Integer>);
procedure Execute; override;
End;
TMyThread2 = Class(TThread)
private
fMySyncAddList1,fMySyncAddList2 : TSimpleEvent;
fMyList1,fMyList2 : TThreadList<Integer>;
fAddVal : Integer;
public
Constructor Create(ASyncAddList1,ASyncAddList2: TSimpleEvent; AList1,AList2 : TThreadList<Integer>);
procedure Execute; override;
End;
TMyThread3 = Class(TThread)
private
fMySyncAddList2 : TSimpleEvent;
fMyList2 : TThreadList<Integer>;
fAddVal : Integer;
public
Constructor Create(ASyncAddList2: TSimpleEvent; AList2 : TThreadList<Integer>);
procedure Execute; override;
End;
{ TMyThread1 }
constructor TMyThread1.Create( ASyncAddList : TSimpleEvent; AList: TThreadList<Integer>);
begin
Inherited Create(false);
fMySyncAddList := AsyncAddList;
fMyList := AList;
end;
procedure TMyThread1.Execute;
var
stateAcknowledged : boolean;
begin
stateAcknowledged := true;
while (not Terminated) do
begin
if stateAcknowledged then
begin // Do some work and adda a value to list1
fAddVal := Random(100);
fMyList.Add(fAddVal);
fMySyncAddList.SetEvent; // Signal a new addition
stateAcknowledged := false;
//ShowVal;
Sleep(1000);
end
else begin
stateAcknowledged := (fMySyncAddList.WaitFor(100) <> wrSignaled);
end;
end;
end;
{ TMyThread2 }
constructor TMyThread2.Create(ASyncAddList1, ASyncAddList2: TSimpleEvent;
AList1, AList2: TThreadList<Integer>);
begin
Inherited Create(false);
fMySyncAddList1 := AsyncAddList1;
fMySyncAddList2 := AsyncAddList2;
fMyList1 := AList1;
fMyList2 := AList2;
end;
procedure TMyThread2.Execute;
var
wr : TWaitResult;
list : TList<Integer>;
pulled : Boolean;
begin
pulled := false;
while (not Terminated) do
begin
if pulled then // Add a value to list2
begin
wr := fMySyncAddList2.WaitFor(0);
if (wr <> wrSignaled) then
begin
fMyList2.Add(fAddVal);
fMySyncAddList2.SetEvent; // Signal a new addition
pulled := false;
end
else Sleep(100);
end
else begin // Wait for a new value in list1
wr := fMySyncAddList1.WaitFor(INFINITE);
if Terminated then
Exit;
if (wr = wrSignaled) then
begin
// Pull out the value
list := fMyList1.LockList;
try
fAddVal := list.Last;
finally
fMyList1.UnlockList;
end;
// All clear
pulled := true;
fMySyncAddList1.ResetEvent;
//ShowVal;
end;
end;
end;
end;
{ TMyThread3 }
constructor TMyThread3.Create(ASyncAddList2: TSimpleEvent;
AList2: TThreadList<Integer>);
begin
Inherited Create(false);
fMySyncAddList2 := AsyncAddList2;
fMyList2 := AList2;
end;
procedure TMyThread3.Execute;
var
wr : TWaitResult;
list : TList<Integer>;
begin
while not Terminated do
begin
wr := fMySyncAddList2.WaitFor(INFINITE);
if Terminated then
Exit;
if (wr = wrSignaled) then // Wait for signal
begin
// Pull out the value
list := fMyList2.LockList;
try
fAddVal := list.Last;
//ShowVal;
finally
fMyList2.UnlockList;
end;
// Clear event
fMySyncAddList2.ResetEvent;
end;
end;
end;
var
list1,list2 : TThreadList<Integer>;
syncList1,syncList2 : TSimpleEvent;
thread1 : TMyThread1;
thread2 : TMyThread2;
thread3 : TMyThread3;
begin
list1 := TThreadList<Integer>.Create;
list2 := TThreadList<Integer>.Create;
syncList1 := TSimpleEvent.Create(Nil,True,False,'',false);
syncList2 := TSimpleEvent.Create(Nil,True,False,'',false);
thread3 := TMyThread3.Create(syncList2,list2);
thread2 := TMyThread2.Create(syncList1,syncList2,list1,list2);
thread1 := TMyThread1.Create(syncList1,list1);
Try
WriteLn('Press [Enter] key to stop.');
ReadLn;
Finally
thread3.Terminate;
syncList2.SetEvent; // Wake up call
thread3.Free;
thread2.Terminate;
syncList1.SetEvent; // Wake up call
thread2.Free;
thread1.Free;
syncList1.Free;
syncList2.Free;
list1.Free;
list2.Free;
End;
end.
您需要一个带有信号量的有界缓冲区,您可以在Martin Harvey的优秀线程教程中找到一个例子:这是一个管道。获取OTL并查看管道示例。正如David所说,OTL可能是您的朋友:'生产者线程在编写之前增加它'-不!写完之后!非常低的水平。你不只是建立一个线程安全队列,并实例化其中的两个,然后每秒醒来10次吗?为什么?@DavidHeffernan,修改了密码以改善睡眠时间。关于不使用队列:线程2似乎希望访问列表1和列表2。不仅是添加的最后一项,而且是整个列表。线程3和列表2也是如此。如果你能排除这一点,那么排队就容易多了。消费者可以提取数据并用数据副本维护自己的私人列表。这是它需要的10倍长,并且使用了令人讨厌的睡眠超时。您不应该在管道中使用睡眠或超时。事实上,睡眠总是应该避免的。对于一对线程安全队列来说,这很简单。拷贝很好,因为它隔离了数据,并且免除了对同步的需要。共享是解决大多数线程复杂性的根本。
program Project63;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
System.SyncObjs,
System.Generics.Collections;
Type
TMyThread1 = Class(TThread)
private
fMyList : TList<Integer>;
fMyQueue : TThreadedQueue<Integer>;
fAddVal : Integer;
public
Constructor Create(AQueue : TThreadedQueue<Integer>);
procedure Execute; override;
End;
TMyThread2 = Class(TThread)
private
fMyList1,fMyList2 : TList<Integer>;
fMyQueue1,fMyQueue2 : TThreadedQueue<Integer>;
fAddVal : Integer;
public
Constructor Create(AQueue1,AQueue2: TThreadedQueue<Integer>);
procedure Execute; override;
End;
TMyThread3 = Class(TThread)
private
fMyList : TList<Integer>;
fMyQueue : TThreadedQueue<Integer>;
fAddVal : Integer;
public
Constructor Create(AQueue : TThreadedQueue<Integer>);
procedure Execute; override;
End;
constructor TMyThread1.Create( AQueue : TThreadedQueue<Integer>);
begin
Inherited Create(false);
fMyQueue:= AQueue;
fMyList := TList<Integer>.Create;
end;
procedure TMyThread1.Execute;
begin
while (not Terminated) do
begin
Sleep(1000); // Simulate some work
fAddVal := Random(100);
fMyList.Add(fAddVal);
fMyQueue.PushItem(fAddVal); // Signal a new addition
end;
fMyList.Free;
end;
constructor TMyThread2.Create(AQueue1,AQueue2: TThreadedQueue<Integer>);
begin
Inherited Create(false);
fMyQueue1 := AQueue1;
fMyQueue2 := AQueue2;
fMyList1 := TList<Integer>.Create;
fMyList2 := TList<Integer>.Create;
end;
procedure TMyThread2.Execute;
var
queueSize : Integer;
begin
while (not Terminated) do
begin
if (fMyQueue1.PopItem(queueSize,fAddVal) = wrSignaled) and
(not Terminated) then
begin
fMyList1.Add(fAddVal);
// Do some work and send a new value to next thread
fMyQueue2.PushItem(fAddVal);
fMyList2.Add(fAddVal);
end;
end;
fMyList1.Free;
fMyList2.Free;
end;
constructor TMyThread3.Create(AQueue : TThreadedQueue<Integer>);
begin
Inherited Create(false);
fMyQueue := AQueue;
fMyList := TList<Integer>.Create;
end;
procedure TMyThread3.Execute;
var
queueSize : Integer;
begin
while not Terminated do
begin
if (fMyQueue.PopItem(queueSize,fAddVal) = wrSignaled) and
(not Terminated) then
begin
fMyList.Add(fAddVal);
// Do some work on list
end;
end;
fMyList.Free;
end;
var
queue1,queue2 : TThreadedQueue<Integer>;
thread1 : TMyThread1;
thread2 : TMyThread2;
thread3 : TMyThread3;
begin
queue1 := TThreadedQueue<Integer>.Create;
queue2 := TThreadedQueue<Integer>.Create;
thread3 := TMyThread3.Create(queue2);
thread2 := TMyThread2.Create(queue1,queue2);
thread1 := TMyThread1.Create(queue1);
Try
WriteLn('Press [Enter] key to stop.');
ReadLn;
Finally
thread3.Terminate;
queue2.PushItem(0); // Wake up call
thread3.Free;
thread2.Terminate;
queue1.PushItem(0); // Wake up call
thread2.Free;
thread1.Free;
queue1.Free;
queue2.Free;
End;
end.