For Programmers: Free Programming Magazines  


Home > Archive > VC Language > January 2006 > WaitForMutltipleObjects behavior









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author WaitForMutltipleObjects behavior
neo

2006-01-26, 7:08 pm

Hi All,

Could someone please help me understanding how does
WaitForMultipleObject behave:

Details: If 2 or more semaphores (on which a thread is executing
WaitForMultipleObjects), is signalled at *the same time* , the return
value of Wait is the index of the first value in the array whose Sem
was signalled. Do the other Semaphores stay in a signalled state and
cause the Wait function to return again ?

thanks in advance,
--Romil

Doug Harrison [MVP]

2006-01-26, 7:08 pm

On 26 Jan 2006 13:55:31 -0800, "neo" <romil.shah@gmail.com> wrote:

>Hi All,
>
>Could someone please help me understanding how does
>WaitForMultipleObject behave:
>
>Details: If 2 or more semaphores (on which a thread is executing
>WaitForMultipleObjects), is signalled at *the same time* , the return
>value of Wait is the index of the first value in the array whose Sem
>was signalled. Do the other Semaphores stay in a signalled state and
>cause the Wait function to return again ?


I think the documentation covers this:

http://msdn.microsoft.com/library/d...ipleobjects.asp
<q>
When bWaitAll is TRUE, the function's wait operation is completed only when
the states of all objects have been set to signaled. The function does not
modify the states of the specified objects until the states of all objects
have been set to signaled. For example, a mutex can be signaled, but the
thread does not get ownership until the states of the other objects are
also set to signaled. In the meantime, some other thread may get ownership
of the mutex, thereby setting its state to nonsignaled.

When bWaitAll is FALSE, this function checks the handles in the array in
order starting with index 0, until one of the objects is signaled. If
multiple objects become signaled, the function returns the index of the
first handle in the array whose object was signaled.

The function modifies the state of some types of synchronization objects.
Modification occurs only for the object or objects whose signaled state
caused the function to return. For example, the count of a semaphore object
is decreased by one. For more information, see the documentation for the
individual synchronization objects.
</q>

To answer your question, if bWaitAll is false, only the first semaphore in
the array will be modified. If bWaitAll is true, both will be modified.

--
Doug Harrison
Visual C++ MVP
neo

2006-01-26, 9:57 pm

Thanks, but I still have some doubts.

>If multiple objects become signaled, the function returns the index of the
>first handle in the array whose object was signaled.


> Modification occurs only for the object or *objects* whose signaled state
> caused the function to return.


But in my scenario,bWaitAll is FALSE and multiple objects are signalled
simultaneously (causing the funtion to return) ..

- Isnt it that multiple synchronization objects being signalled
*caused the function* to return ? In which case, all their states
should be modified !

thanks,
--Romil

Igor Tandetnik

2006-01-26, 9:57 pm

"neo" <romil.shah@gmail.com> wrote in message
news:1138328604.610544.169170@o13g2000cwo.googlegroups.com
> Thanks, but I still have some doubts.
>
>
>
> But in my scenario,bWaitAll is FALSE and multiple objects are
> signalled simultaneously (causing the funtion to return) ..
>
> - Isnt it that multiple synchronization objects being signalled
> *caused the function* to return ? In which case, all their states
> should be modified !


That would be very bad. The function can return only one value
indicating which object got signalled. If it would silently enter more
than one semaphore or acquire several mutexes, you would have no way to
know which ones to release. Deadlock cannot be far away.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Doug Harrison [MVP]

2006-01-26, 9:57 pm

On 26 Jan 2006 18:23:24 -0800, "neo" <romil.shah@gmail.com> wrote:

>Thanks, but I still have some doubts.
>
>
>
>But in my scenario,bWaitAll is FALSE and multiple objects are signalled
>simultaneously (causing the funtion to return) ..


I restored part of the first paragraph you clipped, the pertinent line
containing, "...checks the handles in the array in order starting with
index 0, until one of the objects is signaled." It's looking for one object
to be signaled.

>- Isnt it that multiple synchronization objects being signalled
>*caused the function* to return ? In which case, all their states
>should be modified !


No, "until one of the objects is signaled" indicates it's talking about a
single object. Look at this way. Suppose objects #1, 4, 7, and 9 became
signaled, and WFMO thus returned 1+WAIT_OBJECT_0. Besides object #1, how
would you know which ones to release?

--
Doug Harrison
Visual C++ MVP
neo

2006-01-27, 4:01 am

Thanks a lot for the explanation and being patient :-).

--Romil

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com