multithreading - 为什么 WaitForMultipleObjects 在使用多个线程句柄时会失败?

标签 multithreading delphi delphi-xe2

在下面的测试程序中,每个测试线程在开始执行时将其句柄添加到全局 TThreadList 中,并在执行即将结束时从同一列表中删除其句柄。

出于测试目的,此外,每个线程确保在主线程锁定列表之前添加其句柄(以复制其句柄并开始等待它们完成)。这些线程还确保在主线程锁定列表之前它们不会删除其句柄。

测试程序在最多 50-60 个线程下运行良好。之后,WaitForMultipleObjects 调用开始失败,并显示 WAIT_FAILEDGetLastError 返回 87 (ERROR_INVALID_PARAMETER)。目前它启动了 100 个线程。我的问题是,我做错了什么?

该程序是使用 XE2 - update 4、32 位目标平台编译的。测试盒为W7x64。

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  windows,
  sysutils,
  classes,
  syncobjs;

type
  TTestThread = class(TThread)
  private
    FAckStarted: TEvent;
    function GetAckHandle: THandle;
    class var
      ThreadList: TThreadList;
      WaitEnd: THandle;
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;
    property AckHandle: THandle read GetAckHandle;
  end;

{.$DEFINE FREEONTERMINATE}

constructor TTestThread.Create;
begin
  inherited Create(True);
  FAckStarted := TEvent.Create;
{$IFDEF FREEONTERMINATE}
  FreeOnTerminate := True;
{$ENDIF}
end;

destructor TTestThread.Destroy;
begin
  FAckStarted.Free;
  inherited;
end;

procedure TTestThread.Execute;
begin
//  OutputDebugString(PChar(Format('%d starting -------------', [Handle])));
  ThreadList.Add(Pointer(Handle));
  FAckStarted.SetEvent;

  NameThreadForDebugging(AnsiString(IntToStr(Handle)));

  WaitForSingleObject(WaitEnd, INFINITE);
  ThreadList.Remove(Pointer(Handle));
//  OutputDebugString(PChar(Format('%d leaving -------------', [Handle])));
end;

function TTestThread.GetAckHandle: THandle;
begin
  Result := FAckStarted.Handle;
end;

const
  NumThreads = 100;

var
  DeferThreadEnd: TEvent;
  ThreadList: array of TThread;
  i: Integer;
  Thread: TTestThread;
  WaitThreadStart: THandle;
  LockList: TList;
  LockListCount: Integer;
  ThreadHandleArr: array of THandle;
  WaitRet: DWORD;
begin
  IsMultiThread := True;
  ReportMemoryLeaksOnShutdown := True;

  TTestThread.ThreadList := TThreadList.Create;
  DeferThreadEnd := TEvent.Create;
  TTestThread.WaitEnd := DeferThreadEnd.Handle;

  SetLength(ThreadList, NumThreads);
  for i := 0 to NumThreads - 1 do begin
    Thread := TTestThread.Create;
    ThreadList[i] := Thread;
    WaitThreadStart := Thread.GetAckHandle;
    Thread.Start;
    WaitForSingleObject(WaitThreadStart, INFINITE);
  end;

  LockList := TTestThread.ThreadList.LockList;
  LockListCount := LockList.Count;
  SetLength(ThreadHandleArr, LockListCount);
  for i := 0 to LockListCount - 1 do
{$IFDEF FREEONTERMINATE}
    Win32Check(DuplicateHandle(GetCurrentProcess, THandle(LockList[i]),
            GetCurrentProcess, @ThreadHandleArr[i], SYNCHRONIZE, True, 0));
{$ELSE}
    ThreadHandleArr[i] := THandle(LockList[i]);
{$ENDIF}
  TTestThread.ThreadList.UnlockList;

  DeferThreadEnd.SetEvent;
  if LockListCount > 0 then begin
    Writeln('waiting for ', LockListCount, ' threads');
    WaitRet := WaitForMultipleObjects(LockListCount,
                              PWOHandleArray(ThreadHandleArr), True, INFINITE);
    case WaitRet of
      WAIT_OBJECT_0: Writeln('wait success');
      WAIT_FAILED: Writeln('wait fail:', SysErrorMessage(GetLastError));
    end;
  end;

  for i := 0 to Length(ThreadList) - 1 do begin
{$IFDEF FREEONTERMINATE}
    Win32Check(CloseHandle(ThreadHandleArr[i]));
{$ELSE}
    ThreadList[i].Free;
{$ENDIF}
  end;
  DeferThreadEnd.Free;
  TTestThread.ThreadList.Free;
  Writeln('program end');
  Readln;
end.

最佳答案

WaitForMultipleObjects() documentation状态:

The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.

MAXIMUM_WAIT_OBJECTS的值是 64(在 winnt.h 中定义),因此 100 个句柄超出了限制。但是,该文档还解释了如何克服该限制:

To wait on more than MAXIMUM_WAIT_OBJECTS handles, use one of the following methods:

  • Create a thread to wait on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles. Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS.

  • Call RegisterWaitForSingleObject to wait on each handle. A wait thread from the thread pool waits on MAXIMUM_WAIT_OBJECTS registered objects and assigns a worker thread after the object is signaled or the time-out interval expires.

查看this question的答案有关第一种技术的示例。

关于multithreading - 为什么 WaitForMultipleObjects 在使用多个线程句柄时会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380131/

相关文章:

delphi - 如何判断CoInitialize被调用了多少层?

delphi - 如何检测表单大小调整何时开始和停止?

ios - Delphi XE2/FireMonkey 电子邮件创建

java - Aerospike Java 异步客户端阻塞

java - 微调 Java 同步块(synchronized block)行为

.net - 委托(delegate)和线程问题

delphi - 如何将 "nil"常量传递给无类型参数?

.net - 在没有线程或异步设备的情况下,异步/等待模式是否无关紧要?

c++ - 我在使用 WaitForDebugEvent EXCEPTION_DEBUG_EVENT 时遇到问题

windows - 如何在不禁用子控件功能的情况下从其客户区拖动我的表单?