我目前正在尝试找到让两个线程交替运行并让它们相互等待的最佳(*)方法。
(*) 速度快且 CPU 成本低的最佳组合
到目前为止,我发现了三种方法,我将它们放在一些演示应用程序中以显示我发现的问题。
由于所有锁定(根据 SamplingProfiler 在这些函数中大部分时间都被烧毁),使用遵循经典等待/脉冲模式的 TMonitor 执行得不是很好。我使用 Windows 事件 (SyncObjs.TEvent) 尝试了相同的操作,但它的表现相似(即不好)。
使用调用 TThread.Yield 的等待循环性能最好,但显然会疯狂地消耗 CPU 周期。如果切换发生得很快,这并不重要,但是当线程实际等待时会受到伤害(您可以在演示中看到)。
使用 TSpinWait 性能很好(如果不是这三个中最好的),但前提是切换发生得非常快。由于 TSpinWait 的工作方式,切换所需的时间越长,性能就越差。
由于多线程不是我的优势之一,我想知道是否有这些方式的某种组合或一些完全不同的方法可以在两种情况下(快速和慢速切换)实现良好的性能。
program PingPongThreads;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Classes,
Diagnostics,
SyncObjs,
SysUtils;
type
TPingPongThread = class(TThread)
private
fCount: Integer;
protected
procedure Execute; override;
procedure Pong; virtual;
public
procedure Ping; virtual;
property Count: Integer read fCount;
end;
TPingPongThreadClass = class of TPingPongThread;
TMonitorThread = class(TPingPongThread)
protected
procedure Pong; override;
procedure TerminatedSet; override;
public
procedure Ping; override;
end;
TYieldThread = class(TPingPongThread)
private
fState: Integer;
protected
procedure Pong; override;
public
procedure Ping; override;
end;
TSpinWaitThread = class(TPingPongThread)
private
fState: Integer;
protected
procedure Pong; override;
public
procedure Ping; override;
end;
{ TPingPongThread }
procedure TPingPongThread.Execute;
begin
while not Terminated do
Pong;
end;
procedure TPingPongThread.Ping;
begin
TInterlocked.Increment(fCount);
end;
procedure TPingPongThread.Pong;
begin
TInterlocked.Increment(fCount);
end;
{ TMonitorThread }
procedure TMonitorThread.Ping;
begin
inherited;
TMonitor.Enter(Self);
try
if Suspended then
Start
else
TMonitor.Pulse(Self);
TMonitor.Wait(Self, INFINITE);
finally
TMonitor.Exit(Self);
end;
end;
procedure TMonitorThread.Pong;
begin
inherited;
TMonitor.Enter(Self);
try
TMonitor.Pulse(Self);
if not Terminated then
TMonitor.Wait(Self, INFINITE);
finally
TMonitor.Exit(Self);
end;
end;
procedure TMonitorThread.TerminatedSet;
begin
TMonitor.Enter(Self);
try
TMonitor.Pulse(Self);
finally
TMonitor.Exit(Self);
end;
end;
{ TYieldThread }
procedure TYieldThread.Ping;
begin
inherited;
if Suspended then
Start
else
fState := 3;
while TInterlocked.CompareExchange(fState, 2, 1) <> 1 do
TThread.Yield;
end;
procedure TYieldThread.Pong;
begin
inherited;
fState := 1;
while TInterlocked.CompareExchange(fState, 0, 3) <> 3 do
if Terminated then
Abort
else
TThread.Yield;
end;
{ TSpinWaitThread }
procedure TSpinWaitThread.Ping;
var
w: TSpinWait;
begin
inherited;
if Suspended then
Start
else
fState := 3;
w.Reset;
while TInterlocked.CompareExchange(fState, 2, 1) <> 1 do
w.SpinCycle;
end;
procedure TSpinWaitThread.Pong;
var
w: TSpinWait;
begin
inherited;
fState := 1;
w.Reset;
while TInterlocked.CompareExchange(fState, 0, 3) <> 3 do
if Terminated then
Abort
else
w.SpinCycle;
end;
procedure TestPingPongThread(threadClass: TPingPongThreadClass; quickSwitch: Boolean);
const
MAXCOUNT = 10000;
var
t: TPingPongThread;
i: Integer;
sw: TStopwatch;
w: TSpinWait;
begin
t := threadClass.Create(True);
try
for i := 1 to MAXCOUNT do
begin
t.Ping;
if not quickSwitch then
begin
// simulate some work
w.Reset;
while w.Count < 20 do
w.SpinCycle;
end;
if i = 1 then
begin
if not quickSwitch then
begin
Writeln('Check CPU usage. Press <Enter> to continue');
Readln;
end;
sw := TStopwatch.StartNew;
end;
end;
Writeln(threadClass.ClassName, ' quick switches: ', quickSwitch);
Writeln('Duration: ', sw.ElapsedMilliseconds, ' ms');
Writeln('Call count: ', t.Count);
Writeln;
finally
t.Free;
end;
end;
procedure Main;
begin
TestPingPongThread(TMonitorThread, False);
TestPingPongThread(TYieldThread, False);
TestPingPongThread(TSpinWaitThread, False);
TestPingPongThread(TMonitorThread, True);
TestPingPongThread(TYieldThread, True);
TestPingPongThread(TSpinWaitThread, True);
end;
begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Writeln('Press <Enter> to exit');
Readln;
end.
更新:
我想出了一个事件和 spinwait 的组合:
constructor TSpinEvent.Create;
begin
inherited Create(nil, False, False, '');
end;
procedure TSpinEvent.SetEvent;
begin
fState := 1;
inherited;
end;
procedure TSpinEvent.WaitFor;
var
startCount: Cardinal;
begin
startCount := TThread.GetTickCount;
while TInterlocked.CompareExchange(fState, 0, 1) <> 1 do
begin
if (TThread.GetTickCount - startCount) >= YieldTimeout then // YieldTimeout = 10
inherited WaitFor(INFINITE)
else
TThread.Yield;
end;
end;
在进行快速切换时,这仅比基于光纤的实现慢大约 5 到 6 倍,而在 Ping 调用之间添加一些工作时,速度慢不到 1%。使用光纤时,它当然运行在 2 芯上,而不是仅运行在 1 芯上。