Re: WaitForMultipleObjects + Semaphore Makes Me Uncomfortable
- From: "Kevin" <kevin11@xxxxxxxxxxx>
- Date: Wed, 25 Apr 2007 17:48:48 -0700
"Le Chaud Lapin" <jaibuduvin@xxxxxxxxx> wrote in message
news:1177385676.656114.175010@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi All,
I do not have a "bug" to speak of yet, but something has been
bothering me regarding WaitForMultipleObjects when semaphores are
involved.
I imagine a situation where WaitForMultipleObjects is used against a
plurality of synchronization objects, and at least one of the
synchronization objects is a semaphore guarding a queue. If the mode
of the call is "wait for any object to be signaled", then it is
conceivable that Windows will indicate that a non-semaphore object was
the first to be signaled signaled, while, in addition, the semaphore
also triggered, but the programmer will have no way of knowing that
the semaphore too was signaled. In this case, there will result a
disparity between the count of the semaphore and the count of the
elements in the queue.
When the "other object" was indicated as satisfying the wait, if the
semaphore was signaled at that time, then it will remain signaled until the
next time you wait on it.
This seems conceptually erroneous to me, or suspicious at least. In
my current program, it is not an issue, as, by coincidence, when this
situation occurs, I simply kill the entire queue and the semaphore
along with it. But this is more fortune than deliberate design. I am
sure I can think of situations where it would be a PITA.
Don't access the semaphore count when the other-object is indicated as being
signaled. Just re-enter the wait-function, and it will immediately return
indicating the semaphore is signaled.
Has anyone else had any bad experiences with this? Is there a
workaround?
The fact that only one object at a time can be indicated as being
signaled -- even though other objects may also be signaled at that moment --
is either a curse of the implementation of waiting for multiple objects, or
a feature (the order of objects in the array of handles can have meaning).
You can put the semaphore handle earlier in the array so that if both are
signaled, you will receive the semaphore-indication first.
.
- References:
- WaitForMultipleObjects + Semaphore Makes Me Uncomfortable
- From: Le Chaud Lapin
- WaitForMultipleObjects + Semaphore Makes Me Uncomfortable
- Prev by Date: Re: Find all open handles (Mutex, File, Key etc)
- Next by Date: Re: WaitForMultipleObjects + Semaphore Makes Me Uncomfortable
- Previous by thread: Re: WaitForMultipleObjects + Semaphore Makes Me Uncomfortable
- Next by thread: Re: FlushViewOfFile returns ERROR_LOCK_VIOLATION
- Index(es):