Win32 API 日本語リファレンス
ホームSystem.Threading › WaitForMultipleObjects

WaitForMultipleObjects

関数
複数オブジェクトのいずれかまたは全部のシグナルを待機する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll
#include <windows.h>

WAIT_EVENT WaitForMultipleObjects(
    DWORD nCount,
    const HANDLE* lpHandles,
    BOOL bWaitAll,
    DWORD dwMilliseconds
);

パラメーター

名前方向説明
nCountDWORDinlpHandles が指す配列に含まれるオブジェクトハンドルの数です。オブジェクトハンドルの最大数は MAXIMUM_WAIT_OBJECTS です。このパラメーターを 0 にすることはできません。
lpHandlesHANDLE*in

オブジェクトハンドルの配列です。ハンドルを指定できるオブジェクトの種類の一覧については、後述の「解説」セクションを参照してください。この配列には、異なる種類のオブジェクトのハンドルを含めることができます。同一ハンドルの複製を複数含めることはできません。

待機がまだ保留中の間にこれらのハンドルの 1 つが閉じられた場合、この関数の動作は未定義です。

ハンドルには SYNCHRONIZE アクセス権が必要です。詳細については、 Standard Access Rights を参照してください。

bWaitAllBOOLinこのパラメーターが TRUE の場合、関数は lpHandles 配列内のすべてのオブジェクトの状態がシグナル状態になったときに戻ります。FALSE の場合、関数はいずれか 1 つのオブジェクトの状態がシグナル状態に設定されたときに戻ります。後者の場合、戻り値は関数が戻る原因となったオブジェクトを示します。
dwMillisecondsDWORDin

タイムアウト間隔(ミリ秒単位)です。0 以外の値を指定すると、関数は指定したオブジェクトがシグナル状態になるか間隔が経過するまで待機します。dwMilliseconds が 0 の場合、指定したオブジェクトがシグナル状態でなくても関数は待機状態に入らず、常に即座に戻ります。dwMillisecondsINFINITE の場合、関数は指定したオブジェクトがシグナル状態になったときにのみ戻ります。

Windows XP、Windows Server 2003、Windows Vista、Windows 7、Windows Server 2008、および Windows Server 2008 R2: dwMilliseconds の値には、低電力状態で費やされた時間が含まれます。たとえば、コンピューターがスリープ状態の間もタイムアウトのカウントダウンは継続します。

Windows 8 以降、Windows Server 2012 以降: dwMilliseconds の値には、低電力状態で費やされた時間は含まれません。たとえば、コンピューターがスリープ状態の間はタイムアウトのカウントダウンは継続しません。

戻り値の型: WAIT_EVENT

公式ドキュメント

指定したオブジェクトのいずれか、またはすべてがシグナル状態になるか、タイムアウト間隔が経過するまで待機します。

戻り値

関数が成功した場合、戻り値は関数が戻る原因となったイベントを示します。次のいずれかの値になります。(WAIT_OBJECT_0 は 0 として、WAIT_ABANDONED_0 は 0x00000080L として定義されている点に注意してください。)

戻りコード/値 説明
WAIT_OBJECT_0 から (WAIT_OBJECT_0 + nCount– 1)
bWaitAllTRUE の場合、指定範囲内の戻り値は、指定したすべてのオブジェクトの状態がシグナル状態であることを示します。

bWaitAllFALSE の場合、戻り値から WAIT_OBJECT_0 を引いた値は、待機を満たしたオブジェクトの lpHandles 配列内のインデックスを示します。呼び出し中に複数のオブジェクトがシグナル状態になった場合、これはシグナル状態になったすべてのオブジェクトのうち、最も小さいインデックス値を持つオブジェクトの配列インデックスです。

WAIT_ABANDONED_0 から (WAIT_ABANDONED_0 + nCount– 1)
bWaitAllTRUE の場合、指定範囲内の戻り値は、指定したすべてのオブジェクトの状態がシグナル状態であり、かつ少なくとも 1 つのオブジェクトが放棄された mutex オブジェクトであることを示します。

bWaitAllFALSE の場合、戻り値から WAIT_ABANDONED_0 を引いた値は、待機を満たした放棄された mutex オブジェクトの lpHandles 配列内のインデックスを示します。mutex オブジェクトの所有権は呼び出し元のスレッドに付与され、mutex は非シグナル状態に設定されます。

mutex が永続的な状態情報を保護していた場合は、その整合性を確認する必要があります。

WAIT_TIMEOUT
0x00000102L
タイムアウト間隔が経過し、bWaitAll パラメーターで指定された条件が満たされませんでした。
WAIT_FAILED
(DWORD)0xFFFFFFFF
関数が失敗しました。詳細なエラー情報を取得するには、 GetLastError を呼び出してください。

解説(Remarks)

WaitForMultipleObjects 関数は、待機条件が満たされているかどうかを判定します。条件が満たされていない場合、呼び出し元のスレッドは、待機条件が満たされるかタイムアウト間隔が経過するまで待機状態に入ります。

bWaitAllTRUE の場合、関数の待機操作はすべてのオブジェクトの状態がシグナル状態に設定されたときにのみ完了します。関数は、すべてのオブジェクトの状態がシグナル状態に設定されるまで、指定したオブジェクトの状態を変更しません。たとえば、mutex がシグナル状態であっても、他のオブジェクトの状態もシグナル状態に設定されるまでスレッドは所有権を取得しません。その間に、別のスレッドが mutex の所有権を取得し、その状態を非シグナル状態に設定する場合があります。

bWaitAllFALSE の場合、この関数は配列内のハンドルをインデックス 0 から順に、いずれかのオブジェクトがシグナル状態になるまで確認します。複数のオブジェクトがシグナル状態になった場合、関数はオブジェクトがシグナル状態になった配列内の最初のハンドルのインデックスを返します。

関数は、一部の種類の同期オブジェクトの状態を変更します。変更が行われるのは、シグナル状態が関数を戻す原因となったオブジェクトに対してのみです。たとえば、semaphore オブジェクトのカウントは 1 つ減少します。詳細については、個々の同期オブジェクトのドキュメントを参照してください。

MAXIMUM_WAIT_OBJECTS を超える数のハンドルを待機するには、次のいずれかの方法を使用してください。

WaitForMultipleObjects 関数は、lpHandles 配列内で次のいずれかの種類のオブジェクトのハンドルを指定できます。 待機関数と、直接または間接的にウィンドウを作成するコードを呼び出す際は注意してください。スレッドがウィンドウを作成する場合、そのスレッドはメッセージを処理する必要があります。メッセージのブロードキャストはシステム内のすべてのウィンドウに送信されます。タイムアウト間隔なしで待機関数を使用するスレッドは、システムをデッドロックさせる可能性があります。間接的にウィンドウを作成するコードの例として、DDE と CoInitialize 関数の 2 つが挙げられます。したがって、ウィンドウを作成するスレッドがある場合は、WaitForMultipleObjects ではなく MsgWaitForMultipleObjects または MsgWaitForMultipleObjectsEx を使用してください。

例については、 Waiting for Multiple Objects を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

WAIT_EVENT WaitForMultipleObjects(
    DWORD nCount,
    const HANDLE* lpHandles,
    BOOL bWaitAll,
    DWORD dwMilliseconds
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint WaitForMultipleObjects(
    uint nCount,   // DWORD
    IntPtr lpHandles,   // HANDLE*
    bool bWaitAll,   // BOOL
    uint dwMilliseconds   // DWORD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WaitForMultipleObjects(
    nCount As UInteger,   ' DWORD
    lpHandles As IntPtr,   ' HANDLE*
    bWaitAll As Boolean,   ' BOOL
    dwMilliseconds As UInteger   ' DWORD
) As UInteger
End Function
' nCount : DWORD
' lpHandles : HANDLE*
' bWaitAll : BOOL
' dwMilliseconds : DWORD
Declare PtrSafe Function WaitForMultipleObjects Lib "kernel32" ( _
    ByVal nCount As Long, _
    ByVal lpHandles As LongPtr, _
    ByVal bWaitAll As Long, _
    ByVal dwMilliseconds As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WaitForMultipleObjects = ctypes.windll.kernel32.WaitForMultipleObjects
WaitForMultipleObjects.restype = wintypes.DWORD
WaitForMultipleObjects.argtypes = [
    wintypes.DWORD,  # nCount : DWORD
    ctypes.c_void_p,  # lpHandles : HANDLE*
    wintypes.BOOL,  # bWaitAll : BOOL
    wintypes.DWORD,  # dwMilliseconds : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
WaitForMultipleObjects = Fiddle::Function.new(
  lib['WaitForMultipleObjects'],
  [
    -Fiddle::TYPE_INT,  # nCount : DWORD
    Fiddle::TYPE_VOIDP,  # lpHandles : HANDLE*
    Fiddle::TYPE_INT,  # bWaitAll : BOOL
    -Fiddle::TYPE_INT,  # dwMilliseconds : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn WaitForMultipleObjects(
        nCount: u32,  // DWORD
        lpHandles: *mut *mut core::ffi::c_void,  // HANDLE*
        bWaitAll: i32,  // BOOL
        dwMilliseconds: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern uint WaitForMultipleObjects(uint nCount, IntPtr lpHandles, bool bWaitAll, uint dwMilliseconds);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_WaitForMultipleObjects' -Namespace Win32 -PassThru
# $api::WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
#uselib "KERNEL32.dll"
#func global WaitForMultipleObjects "WaitForMultipleObjects" sptr, sptr, sptr, sptr
; WaitForMultipleObjects nCount, lpHandles, bWaitAll, dwMilliseconds   ; 戻り値は stat
; nCount : DWORD -> "sptr"
; lpHandles : HANDLE* -> "sptr"
; bWaitAll : BOOL -> "sptr"
; dwMilliseconds : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global WaitForMultipleObjects "WaitForMultipleObjects" int, sptr, int, int
; res = WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
; nCount : DWORD -> "int"
; lpHandles : HANDLE* -> "sptr"
; bWaitAll : BOOL -> "int"
; dwMilliseconds : DWORD -> "int"
; WAIT_EVENT WaitForMultipleObjects(DWORD nCount, HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
#uselib "KERNEL32.dll"
#cfunc global WaitForMultipleObjects "WaitForMultipleObjects" int, intptr, int, int
; res = WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
; nCount : DWORD -> "int"
; lpHandles : HANDLE* -> "intptr"
; bWaitAll : BOOL -> "int"
; dwMilliseconds : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procWaitForMultipleObjects = kernel32.NewProc("WaitForMultipleObjects")
)

// nCount (DWORD), lpHandles (HANDLE*), bWaitAll (BOOL), dwMilliseconds (DWORD)
r1, _, err := procWaitForMultipleObjects.Call(
	uintptr(nCount),
	uintptr(lpHandles),
	uintptr(bWaitAll),
	uintptr(dwMilliseconds),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WAIT_EVENT
function WaitForMultipleObjects(
  nCount: DWORD;   // DWORD
  lpHandles: Pointer;   // HANDLE*
  bWaitAll: BOOL;   // BOOL
  dwMilliseconds: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'WaitForMultipleObjects';
result := DllCall("KERNEL32\WaitForMultipleObjects"
    , "UInt", nCount   ; DWORD
    , "Ptr", lpHandles   ; HANDLE*
    , "Int", bWaitAll   ; BOOL
    , "UInt", dwMilliseconds   ; DWORD
    , "UInt")   ; return: WAIT_EVENT
●WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds) = DLL("KERNEL32.dll", "dword WaitForMultipleObjects(dword, void*, bool, dword)")
# 呼び出し: WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
# nCount : DWORD -> "dword"
# lpHandles : HANDLE* -> "void*"
# bWaitAll : BOOL -> "bool"
# dwMilliseconds : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn WaitForMultipleObjects(
    nCount: u32, // DWORD
    lpHandles: ?*anyopaque, // HANDLE*
    bWaitAll: i32, // BOOL
    dwMilliseconds: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc WaitForMultipleObjects(
    nCount: uint32,  # DWORD
    lpHandles: pointer,  # HANDLE*
    bWaitAll: int32,  # BOOL
    dwMilliseconds: uint32  # DWORD
): uint32 {.importc: "WaitForMultipleObjects", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint WaitForMultipleObjects(
    uint nCount,   // DWORD
    void* lpHandles,   // HANDLE*
    int bWaitAll,   // BOOL
    uint dwMilliseconds   // DWORD
);
ccall((:WaitForMultipleObjects, "KERNEL32.dll"), stdcall, UInt32,
      (UInt32, Ptr{Cvoid}, Int32, UInt32),
      nCount, lpHandles, bWaitAll, dwMilliseconds)
# nCount : DWORD -> UInt32
# lpHandles : HANDLE* -> Ptr{Cvoid}
# bWaitAll : BOOL -> Int32
# dwMilliseconds : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t WaitForMultipleObjects(
    uint32_t nCount,
    void* lpHandles,
    int32_t bWaitAll,
    uint32_t dwMilliseconds);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
-- nCount : DWORD
-- lpHandles : HANDLE*
-- bWaitAll : BOOL
-- dwMilliseconds : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const WaitForMultipleObjects = lib.func('__stdcall', 'WaitForMultipleObjects', 'uint32_t', ['uint32_t', 'void *', 'int32_t', 'uint32_t']);
// WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
// nCount : DWORD -> 'uint32_t'
// lpHandles : HANDLE* -> 'void *'
// bWaitAll : BOOL -> 'int32_t'
// dwMilliseconds : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  WaitForMultipleObjects: { parameters: ["u32", "pointer", "i32", "u32"], result: "u32" },
});
// lib.symbols.WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds)
// nCount : DWORD -> "u32"
// lpHandles : HANDLE* -> "pointer"
// bWaitAll : BOOL -> "i32"
// dwMilliseconds : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t WaitForMultipleObjects(
    uint32_t nCount,
    void* lpHandles,
    int32_t bWaitAll,
    uint32_t dwMilliseconds);
C, "KERNEL32.dll");
// $ffi->WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds);
// nCount : DWORD
// lpHandles : HANDLE*
// bWaitAll : BOOL
// dwMilliseconds : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    int WaitForMultipleObjects(
        int nCount,   // DWORD
        Pointer lpHandles,   // HANDLE*
        boolean bWaitAll,   // BOOL
        int dwMilliseconds   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun WaitForMultipleObjects = WaitForMultipleObjects(
    nCount : UInt32,   # DWORD
    lpHandles : Void*,   # HANDLE*
    bWaitAll : Int32,   # BOOL
    dwMilliseconds : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WaitForMultipleObjectsNative = Uint32 Function(Uint32, Pointer<Void>, Int32, Uint32);
typedef WaitForMultipleObjectsDart = int Function(int, Pointer<Void>, int, int);
final WaitForMultipleObjects = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<WaitForMultipleObjectsNative, WaitForMultipleObjectsDart>('WaitForMultipleObjects');
// nCount : DWORD -> Uint32
// lpHandles : HANDLE* -> Pointer<Void>
// bWaitAll : BOOL -> Int32
// dwMilliseconds : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WaitForMultipleObjects(
  nCount: DWORD;   // DWORD
  lpHandles: Pointer;   // HANDLE*
  bWaitAll: BOOL;   // BOOL
  dwMilliseconds: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'WaitForMultipleObjects';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WaitForMultipleObjects"
  c_WaitForMultipleObjects :: Word32 -> Ptr () -> CInt -> Word32 -> IO Word32
-- nCount : DWORD -> Word32
-- lpHandles : HANDLE* -> Ptr ()
-- bWaitAll : BOOL -> CInt
-- dwMilliseconds : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let waitformultipleobjects =
  foreign "WaitForMultipleObjects"
    (uint32_t @-> (ptr void) @-> int32_t @-> uint32_t @-> returning uint32_t)
(* nCount : DWORD -> uint32_t *)
(* lpHandles : HANDLE* -> (ptr void) *)
(* bWaitAll : BOOL -> int32_t *)
(* dwMilliseconds : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("WaitForMultipleObjects" wait-for-multiple-objects :convention :stdcall) :uint32
  (n-count :uint32)   ; DWORD
  (lp-handles :pointer)   ; HANDLE*
  (b-wait-all :int32)   ; BOOL
  (dw-milliseconds :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WaitForMultipleObjects = Win32::API::More->new('KERNEL32',
    'DWORD WaitForMultipleObjects(DWORD nCount, HANDLE lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)');
# my $ret = $WaitForMultipleObjects->Call($nCount, $lpHandles, $bWaitAll, $dwMilliseconds);
# nCount : DWORD -> DWORD
# lpHandles : HANDLE* -> HANDLE
# bWaitAll : BOOL -> BOOL
# dwMilliseconds : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目
使用する型