Ola*_*ien 22 delphi multithreading
Delphi中的匿名方法创建了一个闭包,它将"周围"的局部变量保存在上下文中,直到匿名方法完成.如果使用接口变量,那么它们将在匿名方法完成之前减少其引用的实例.到现在为止还挺好.
当使用带有匿名方法的TTask.Run(AProc:TProc)时,我希望在关联的工作线程完成执行"AProc"时释放闭包.但这似乎没有发生.在程序终止时,当线程池(此TTask生成的线程所属的)被释放时,您终于可以看到这些本地引用的实例被释放 - 即闭包显然已被释放.
问题是这是一个功能还是一个bug?或者我在这里监督一些事情?
下面,在TTask.Run(...)之后.等待我希望调用LFoo的析构函数 - 这不会发生.
procedure Test3;
var
LFoo: IFoo;
begin
LFoo := TFoo.Create;
TTask.Run(
procedure
begin
Something(LFoo);
end).Wait; // Wait for task to finish
//After TTask.Run has finished, it should let go LFoo out of scope - which it does not apprently.
end;
Run Code Online (Sandbox Code Playgroud)
以下是一个完整的测试用例,它表明一个"简单"的匿名方法按预期工作(Test2),但是当输入TTask.Run时它没有(Test3)
program InterfaceBug;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Classes,
System.SysUtils,
System.Threading;
type
//Simple Interface/Class
IFoo = interface(IInterface)
['{7B78D718-4BA1-44F2-86CB-DDD05EF2FC56}']
procedure Bar;
end;
TFoo = class(TInterfacedObject, IFoo)
public
constructor Create;
destructor Destroy; override;
procedure Bar;
end;
procedure TFoo.Bar;
begin
Writeln('Foo.Bar');
end;
constructor TFoo.Create;
begin
inherited;
Writeln('Foo.Create');
end;
destructor TFoo.Destroy;
begin
Writeln('Foo.Destroy');
inherited;
end;
procedure Something(const AFoo: IFoo);
begin
Writeln('Something');
AFoo.Bar;
end;
procedure Test1;
var
LFoo: IFoo;
begin
Writeln('Test1...');
LFoo := TFoo.Create;
Something(LFoo);
Writeln('Test1 done.');
//LFoo goes out od scope, and the destructor gets called
end;
procedure Test2;
var
LFoo: IFoo;
LProc: TProc;
begin
Writeln('Test2...');
LFoo := TFoo.Create;
LProc := procedure
begin
Something(LFoo);
end;
LProc();
Writeln('Test2 done.');
//LFoo goes out od scope, and the destructor gets called
end;
procedure Test3;
var
LFoo: IFoo;
begin
Writeln('Test3...');
LFoo := TFoo.Create;
TTask.Run(
procedure
begin
Something(LFoo);
end).Wait; // Wait for task to finish
//LFoo := nil; This would call TFoo's destructor,
//but it should get called automatically with LFoo going out of scope - which apparently does not happen!
Writeln('Test3 done.');
end;
begin
try
Test1; //works
Writeln;
Test2; //works
Writeln;
Test3; //fails
Writeln('--------');
Writeln('Expected: Three calls of Foo.Create and three corresponding ones of Foo.Destroy');
Writeln;
Writeln('Actual: The the third Foo.Destroy is missing and is executed when the program terminates, i.e. when the default ThreadPool gets destroyed.');
ReadLn;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Run Code Online (Sandbox Code Playgroud)
Ste*_*nke 19
我这样做是错误的一些分析,以找出原因的真正原因ITask被关押在TThreadPool.TQueueWorkerThread.Execute如提到的已知问题.
以下无辜的代码行是问题:
Item := ThreadPool.FQueue.Dequeue;
Run Code Online (Sandbox Code Playgroud)
为什么会这样?因为TQueue<T>.Dequeue标记为内联,现在您必须知道编译器不会对返回托管类型的内联函数应用所谓的返回值优化.
这意味着编译器在真正转换(我非常简化了这个)代码之前的行.tmp是一个编译器生成的变量 - 它在方法的序言中保留堆栈上的空间:
tmp := ThreadPool.FQueue.Dequeue;
Item := tmp;
Run Code Online (Sandbox Code Playgroud)
该变量end在方法中最终确定.你可以在那里放一个断点TTask.Destroy,然后你会看到当应用程序一旦到达方法结束就结束了,这将触发最后一个TTask实例被破坏,因为正在保持它活着的临时变量被清除.
我用了一些小黑客来解决这个问题.我添加了这个本地过程来消除临时变量潜入TThreadPool.TQueueWorkerThread.Execute方法:
procedure InternalDequeue(var Item: IThreadPoolWorkItem);
begin
Item := ThreadPool.FQueue.Dequeue;
end;
Run Code Online (Sandbox Code Playgroud)
然后更改方法内的代码:
InternalDequeue(Item);
Run Code Online (Sandbox Code Playgroud)
这仍然会导致Dequeue生成一个临时变量,但现在它只存在于InternalDequeue方法内部,并在退出后被清除.
编辑(09.11.2017):在编译器中已在10.2中修复.它现在在将temp变量赋值给真实变量之后插入finally块,因此temp变量不会引起额外的引用.
Dal*_*kar 18
这是已知问题:TThreadPool工作线程保存对上次执行任务的引用
TThreadPool.TQueueWorkerThread.Execute中的临时变量保留对最后执行的工作项(任务)的引用,该工作项仅在Execute方法结束时释放.
在池中,线程通常保持活动状态,直到池被破坏为止,默认池意味着在完成单元期间.因此,在程序终止之前,不会释放最后执行的任务.