Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Delphi 2007 AsyncMultiSync不工作_Multithreading_Delphi_Delphi 2007 - Fatal编程技术网

Multithreading Delphi 2007 AsyncMultiSync不工作

Multithreading Delphi 2007 AsyncMultiSync不工作,multithreading,delphi,delphi-2007,Multithreading,Delphi,Delphi 2007,我试着用s编写一个并行线程示例,下面是并行计算素数的数量 program Project3; {$APPTYPE CONSOLE} uses SysUtils, Math, Windows, AsyncCalls in 'AsyncCalls.pas'; const N = 1000000; MAXT = 100; var threads : array of IAsyncCall; ans: array of integer; cnt: DWORD;

我试着用s编写一个并行线程示例,下面是并行计算素数的数量

program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Math,
  Windows,
  AsyncCalls in 'AsyncCalls.pas';

const
  N = 1000000;
  MAXT = 100;

var
  threads : array of IAsyncCall;
  ans: array of integer;
  cnt: DWORD;
  i, v, j, k, portion: integer;

function IsPrime(x: integer): boolean;
var
  i: integer;
begin
  if (x <= 1) then 
  begin
    Result := False;
    Exit;
  end;
  if (x = 2) then
  begin
    Result := True;
    Exit;
  end;
  for i:= 2 to Ceil(Sqrt(x))do
  begin
    if (x mod i = 0) then
    begin
      Result := False;
      Exit;
    end;
  end;
  Result := True;
end;

procedure DoWork(left, right: integer; value: PInteger); cdecl;
var
  i, cnt: integer;
begin
  cnt := 0;
  for i := left to right do
  begin
    if (IsPrime(i)) then
    begin
      Inc(cnt);
    end;
  end;
  value^ := cnt;
end;

begin
  // Paralell
  cnt := GetTickCount;
  SetLength(ans, MAXT);
  SetLength(threads, MAXT);
  portion := N div MAXT;
  for i := 0 to MAXT - 2 do
  begin
    // left index
    j := i * portion;
    // right index
    k := (i + 1) * portion - 1;
    threads[i] := AsyncCall(@DoWork, [j, k, @ans[i]]);
  end;
  // last thread
  j := (MAXT - 1) * portion;
  threads[MAXT - 1] := AsyncCall(@DoWork, [j, N - 1, @ans[MAXT - 1]]);
  // Join, doesn't seem to wait all
  AsyncMultiSync(threads, True, INFINITE);
  // ****Adding a delay to wait for all threads*****
  // Sleep(1000);
  // Sum the answer
  v := 0;
  for i := 0 to MAXT - 1 do
  begin
    Inc(v, ans[i]);
  end;
  Writeln('Parallel = ', GetTickCount - cnt);
  Writeln('Answer = ', v);  
  // Serial
  cnt := GetTickCount;
  DoWork(0, N - 1, @v);
  Writeln('Serial = ', GetTickCount - cnt);
  Writeln('Answer = ', v);  
  Readln;
end.
如果添加睡眠(1000),则输出正确:

Parallel = 1188
Answer = 78498
Serial = 265
Answer = 78498
我尝试使用threads[I].Sync,它产生了类似的结果

我错过什么了吗

环境是D2007,Windows 10 64位Home

表示不超过最大等待对象数61


因此,您必须减少
MaxT
常量。

您不检查AsyncMultiSync的返回值。即使使用无限参数?文档中说:
函数在所有列出的异步调用完成后返回。如果毫秒是无限的,异步调用将在当前线程中执行。当所有异步调用完成时,返回值为零。否则等待失败。
谢谢!没想到会这样。是的,在我将MAXT设置为61后,它会按预期工作。
Parallel = 1188
Answer = 78498
Serial = 265
Answer = 78498