Are Events fair?

May 07th, 2009 - 07:10 pm ET by Bruce. | Report spam
If I have a multiprocess shared Event (CreateEvent) and there are multiple
threads in these processes all blocked (WaitForSingleObject) on this
auto-reset Event, and if I then call SetlEvent (once per blocked thread),
will the blocked threads be released in the order they blocked?

If it is normal for the blocked threads to be released in the order they
block, can Windows modify that release order if the blocked threads have
differring thread priorities?

Thanks,
Bruce.
email Follow the discussionReplies 10 repliesReplies Make a reply

Similar topics

Replies

#1 Chris Taylor
May 10th, 2009 - 05:43 am ET | Report spam
Hi,

Threads waiting on an event take ownership of the lock on FIFO bases. When a
thread signals an event the first thread in the wait list for the event will
take ownership of the lock. I would not recommend that you rely on this
logic in your code however.

Hope this helps

Chris Taylor
http://taylorza.blogspot.com
http://dotnetjunkies.com/weblog/chris.taylor



"Bruce." wrote in message
news:#
If I have a multiprocess shared Event (CreateEvent) and there are multiple
threads in these processes all blocked (WaitForSingleObject) on this
auto-reset Event, and if I then call SetlEvent (once per blocked thread),
will the blocked threads be released in the order they blocked?

If it is normal for the blocked threads to be released in the order they
block, can Windows modify that release order if the blocked threads have
differring thread priorities?

Thanks,
Bruce.




Replies Reply to this message
#2 Bruce.
May 11th, 2009 - 08:33 am ET | Report spam
"Chris Taylor" wrote in message
news:
Hi,

Threads waiting on an event take ownership of the lock on FIFO bases. When
a thread signals an event the first thread in the wait list for the event
will take ownership of the lock. I would not recommend that you rely on
this logic in your code however.



Thank you very much!

Bruce.
Replies Reply to this message
#3 Marc Sherman
May 11th, 2009 - 09:22 am ET | Report spam
IIRC, if a waiting thread runs an APC, then it will be placed at the end of
the wait queue when it's done with the APC. That breaks the FIFO ordering.

"Bruce." wrote in message
news:
"Chris Taylor" wrote in message
news:
Hi,

Threads waiting on an event take ownership of the lock on FIFO bases.
When a thread signals an event the first thread in the wait list for the
event will take ownership of the lock. I would not recommend that you
rely on this logic in your code however.



Thank you very much!

Bruce.




Replies Reply to this message
#4 Alexander Grigoriev
May 11th, 2009 - 10:22 am ET | Report spam
As others mentioned, there is some failrness, but it's an implementation
detail and could possibly change in another OS version. And there is no
fairness guarantee even in the current version. Current implementation also
doesn't take priorities into account, but that could change, too.

"Bruce." wrote in message
news:%
If I have a multiprocess shared Event (CreateEvent) and there are multiple
threads in these processes all blocked (WaitForSingleObject) on this
auto-reset Event, and if I then call SetlEvent (once per blocked thread),
will the blocked threads be released in the order they blocked?

If it is normal for the blocked threads to be released in the order they
block, can Windows modify that release order if the blocked threads have
differring thread priorities?

Thanks,
Bruce.




Replies Reply to this message
#5 m
May 11th, 2009 - 07:22 pm ET | Report spam
Do not rely on the 'fairness' of auto-reset events. The interface
guarantees only that ONE of the threads waiting on the event (or about to
wait on the event) will acquire it and reset the event.



If the threads, once they have acquired the lock, never wait on it again,
you are okay; or if there is only ever ONE thread waiting on the event you
are okay; but otherwise, events do not provide this kind of sync and should
not be used. Many factors beyond ordinary programmatic control may cause
the lock to be acquired by a thread that you do not expect.




"Bruce." wrote in message
news:%
If I have a multiprocess shared Event (CreateEvent) and there are multiple
threads in these processes all blocked (WaitForSingleObject) on this
auto-reset Event, and if I then call SetlEvent (once per blocked thread),
will the blocked threads be released in the order they blocked?

If it is normal for the blocked threads to be released in the order they
block, can Windows modify that release order if the blocked threads have
differring thread priorities?

Thanks,
Bruce.




Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search