Skip to main content
gamedev.net
🔒 Locked

WaitForMultipleObjects

Started by Greg K 4 replies 3.6k views
Original Post
Greg K
Greg K
When using WaitForMultipleObjects, how does the function respond when one or more of the elements in the array of handles you wish to wait for are NULL? -Greg
LessBread
LessBread
Probably by returning -1 (ie WAIT_FAILED). If you're confronting that return value, call GetLastError and plug the result from that into FormatMessage to get a error message string with information about the error. See Retrieving the Last-Error Code for sample code.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Greg K
Greg K
handle is an array of handles.

for( int i = 0; i < MAX_THREADS; i++ )    handle = CreateEvent(null, true, true, null);...DWORD index = WaitForMultipleObjects(MAX_THREADS, handle, false, INFINITE);if( index != WAIT_FAILED ){    CloseHandle(handle[index-WAIT_OBJECT_0]);    handle[index-WAIT_OBJECT_0] = CreateEvent(null, true, false, null);}


WaitForMultipleObjects always gives me -1. What am I doing wrong?
-Greg
Evil Steve
Evil Steve
That exact code works fine for me. On a side note, you don't need to destroy and re-create the event. You can use ResetEvent() to set the state of the event to nonsignalled.
Perhaps your handles are being closed somewhere else in your code (but not set to NULL)? That'll cause GetLastError() to give you an error about an invalid handle I expect.
Greg K
Greg K
Good point. I have changed my code accordingly, however I am still only getting WAIT_FAILED and the extended error message is:
failed with error 87: The parameter is incorrect.

What parameter is incorrect? They all look correct.

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

#define MAX_THREADS 128
...
HANDLE handle[MAX_THREADS];
...
DWORD index = WaitForMultipleObjects(MAX_THREADS, handle, false, INFINITE);
Greg K
Greg K
Fixed:
#define MAX_THREADS MAXIMUM_WAIT_OBJECTS

Max wait objects must be less than 128...

Now everything works fine. Thanks for your help guys.
-Greg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.