为了尝试使用Delphi中的线程库并行计算任务并使用TTask.WaitForAny()
获取第一个计算结果,异常暂时停止了执行.
在例外情况下调用堆栈:
$ 752D2F71的首次机会异常.异常类EMonitorLockException,消息'Object lock not owned'.处理Project1.exe(11248)
:752d2f71 KERNELBASE.RaiseException + 0x48
System.TMonitor.CheckOwningThread
System.ErrorAt(25,$408C70)
System.Error(reMonitorNotLocked)
System.TMonitor.CheckOwningThread
System.TMonitor.Exit
System.TMonitor.Exit($2180E40)
System.Threading.TTask.RemoveCompleteEvent(???)
System.Threading.TTask.DoWaitForAny((...),4294967295)
System.Threading.TTask.WaitForAny((...))
Project9.Parallel2
Project9.Project1
:74ff919f KERNEL32.BaseThreadInitThunk + 0xe
:7723b54f ntdll.RtlInitializeExceptionChain + 0x8f
:7723b51a ntdll.RtlInitializeExceptionChain + 0x5a
Run Code Online (Sandbox Code Playgroud)
调用堆栈得出的结论是异常是由线程库中的错误引起的,TMonitor
和/或TTask.WaitForAny()
.为了验证这一点,代码被减少到最低限度:
program Project1;
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.Threading, System.Classes, System.SyncObjs,
System.StrUtils;
var
WorkerCount : integer = 1000;
function MyTaskProc: TProc;
begin
result := procedure
begin
// Do something
end;
end;
procedure Parallel2;
var
i : Integer;
Ticks: Cardinal;
tasks: array …
Run Code Online (Sandbox Code Playgroud)