gpt4 book ai didi

delphi - 如何销毁/释放和匿名线程

转载 作者:行者123 更新时间:2023-12-02 07:22:43 28 4
gpt4 key购买 nike

在 Windows 上,要停止并销毁匿名线程,我只需执行 FmyTask.free => ,它将调用 destroy => 并且在销毁内部将设置 < strong>terminate = true并调用waitfor等待任务完成=>最后清理使用的内存

但是在 ARC 上,一切都不同了:(我使用这个代码:

TMyObject
private
FMyTask: TThread;
public
destructor Destroy; override;
procedure DoSomething;
end;

destructor TMyObject.Destroy;
begin
FMyTask.free; // << will do nothing because FMyTask.refcount = 2 !! how it's possible ?
FMyTask:= nil;
inherited;
end;

procedure TMyObject.DoSomething;
begin

FMyTask:= Thread.CreateAnonymousThread(
procedure
begin
sleep(10000000);
end);
FMyTask.FreeOnTerminate := False;
FMyTask.Start;

end;

我什么也不做

MyObject := TmyObject.create;
MyObject.DoSomething;
MyObject.free;
MyObject := nil;

但是正如你所看到的,在 TmyObject 的 onDestroy 中,FMyTask 的引用计数为 2!所以这意味着 FMyTask 不会被 TmyObject 破坏(引用计数将减少到 1)(但稍后),您可以想象可能导致的所有错误:(

很显然,对 fMyTask 的引用保留在该函数内:

function ThreadProc(const Thread: TThread): Integer;
var
FreeThread: Boolean;
{$IFDEF MACOS}
pool: Pointer;
{$ENDIF MACOS}
begin
{$IFDEF AUTOREFCOUNT}
Thread.__ObjAddRef; // this ensures the instance remains for as long as the thread is running
{$ENDIF}
TThread.FCurrentThread := Thread;
{$IF Defined(POSIX)}
if Thread.FSuspended then
pthread_mutex_lock(Thread.FCreateSuspendedMutex);
{$ENDIF POSIX}
{$IFDEF MACOS}
// Register the auto release pool
pool := objc_msgSend(objc_msgSend(objc_getClass('NSAutoreleasePool'),
sel_getUid('alloc')), sel_getUid('init'));
{$ENDIF MACOS}
try
Thread.FStarted := True;
if not Thread.Terminated then
try
Thread.Execute;
except
Thread.FFatalException := AcquireExceptionObject;
end;
finally
Result := Thread.FReturnValue;
FreeThread := Thread.FFreeOnTerminate;
Thread.DoTerminate;
Thread.FFinished := True;
SignalSyncEvent;
if FreeThread then
begin
Thread.DisposeOf;
{$IFDEF AUTOREFCOUNT}
Thread.__ObjRelease; // This will clear the thread reference that was added by setting FreeOnTerminate.
{$ENDIF}
end;
{$IFDEF AUTOREFCOUNT}
Thread.__ObjRelease; // This will clear the thread reference we added above. This may initiate disposal.
{$ENDIF}
{$IFDEF USE_LIBICU}
// Destroy Collator Cache
ClearCollatorCache;
{$ENDIF}
{$IF Defined(MSWINDOWS)}
EndThread(Result);
{$ELSEIF Defined(POSIX)}
{$IFDEF MACOS}
// Last thing to do in thread is to drain the pool
objc_msgSend(pool, sel_getUid('drain'));
{$ENDIF MACOS}
{$IFDEF ANDROID}
// Detach the NativeActivity virtual machine to ensure the proper relase of JNI context attached to the current thread
PJavaVM(System.JavaMachine)^.DetachCurrentThread(PJavaVM(System.JavaMachine));
{$ENDIF ANDROID}
// Directly call pthread_exit since EndThread will detach the thread causing
// the pthread_join in TThread.WaitFor to fail. Also, make sure the EndThreadProc
// is called just like EndThread would do. EndThreadProc should not return
// and call pthread_exit itself.
if Assigned(EndThreadProc) then
EndThreadProc(Result);
pthread_exit(Result);
{$ENDIF POSIX}
end;
end;

这是正常行为吗?如果是的话,我是唯一一个认为 arc 比 delphi 更糟糕的人?

最佳答案

ARC 必须保留对线程的额外引用,以便在创建者超出范围时不会过早释放它。

在非 ARC 下,当您调用 FMyThread.Free 时,会调用 Destroy 析构函数,然后调用 Terminate;WaitFor ;

使用 ARC,调用 Free 会减少引用计数并检查是否为零。由于还剩下一个引用,因此不会调用析构函数。

这意味着释放线程的模式(使用FreeOnTerminate=false)应该是:

fMyThread.Terminate;
fMyThread.WaitFor; // Wait for the thread to finish
fMyThread.Free;

这意味着 ARC 获取的额外引用将被删除,并且线程对象将在创建者超出范围后随时释放。

<小时/>

请注意,这种释放线程的模式(使用FreeOnTerminate=false)适用于所有编译器,并且还可以避免在非 ARC 区域中终止线程时出现罕见的竞争条件。

<小时/>

所有设置为 FreeOnTerminate = true 的线程的一般规则是它们不应通过引用从外部访问。

关于delphi - 如何销毁/释放和匿名线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39931796/

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