小编Jim*_*ean的帖子

如何从新的线程库中使用TTask.WaitForAny?

为了尝试使用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)

delphi multithreading delphi-xe7 rtl-ppl

7
推荐指数
1
解决办法
2811
查看次数

Delphi XE7如何处理并行任务中的多线程

我是并行编程的新手,我试图找出为什么偶尔会得到一个EmonitorLockException:当我增加要运行的并行任务的数量时,不拥有对象锁.是这样的情况,线程变得纠结我运行的任务越多.或者我的代码不正确?

{$APPTYPE CONSOLE}

uses
System.SysUtils, System.Threading, System.Classes, System.SyncObjs, System.StrUtils;

const

WorkerCount = 10000; // this is the number of tasks to run in parallel    note:when this number is increased by repeated factors of 10
                  // it takes longer to produce the result and sometimes the program crashes
                  // with an EmonitorLockException:Object lock not owned . Is it that my program is not written correctly or efficiently to run
                  // a large number of parallel taks and the Threads become entagled. …
Run Code Online (Sandbox Code Playgroud)

delphi parallel-processing multithreading task

0
推荐指数
1
解决办法
2620
查看次数