468,133 Members | 1,124 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

One ManualResetEvent, multiple threads waiting

X-No-Archive: Yes
From what I understand, if multiple threads are waiting on a

ManualResetEvent after calling WaitOne(), only one thread is guaranteed
to be signaled when Set() is called on that ManualResetEvent. How do I
ensure that _all_ waiting threads are signaled, without sacrificing any
of the functionality or much of the simplicity of ManualResetEvent?

Example:

SignalingClass:
ManualResetEvent handle = new ManualResetEvent(false);

void SomeRandomMethod() { handle.Set(); }

Thread #1: SignalingClassObject.handle.WaitOne();
Thread #2: SignalingClassObject.handle.WaitOne();

Nov 17 '05 #1
3 12058
It is my understanding that all current waiting threads are moved to the
ready queue, so they will run when the OS schedules them. Any other threads
will go right to ready queue until the event is reset(). Are you not seeing
this behavior?

--
William Stacey [MVP]

"Elhurzen" <66*******@sneakemail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
X-No-Archive: Yes
From what I understand, if multiple threads are waiting on a

ManualResetEvent after calling WaitOne(), only one thread is guaranteed
to be signaled when Set() is called on that ManualResetEvent. How do I
ensure that _all_ waiting threads are signaled, without sacrificing any
of the functionality or much of the simplicity of ManualResetEvent?

Example:

SignalingClass:
ManualResetEvent handle = new ManualResetEvent(false);

void SomeRandomMethod() { handle.Set(); }

Thread #1: SignalingClassObject.handle.WaitOne();
Thread #2: SignalingClassObject.handle.WaitOne();

Nov 17 '05 #2

I've noticed an error in my original question after checking with MSDN
docs.

MSDN on ManualResetEvent:
"Threads that call WaitOne on the ManualResetEvent will block, awaiting
the signal. When the controlling thread completes the activity, it
calls Set to signal that the waiting threads can proceed. *** All
waiting threads are released. ***"

But for AutoResetEvent, MSDN says:
"Calling Set signals AutoResetEvent to release a waiting thread.
AutoResetEvent remains signaled *** until a single waiting thread is
released ***, and then automatically returns to the nonsignaled state.
If no threads are waiting, the state remains signaled indefinitely."

I read the AutoResetEvent description and assumed that the same would
apply to ManualResetEvent.

But ManualResetEvent wakes up ALL waiting threads while AutoResetEvent
might move on after waking up just one. I haven't actually been able to
try this, but please correct me if I've understood this incorrectly.

Why is there such a discrepancy? Why can't AutoResetEvent wake up all
of its own waiting threads?

In any case, my "problem" with ManualResetEvent seems to be solved. :-)

Nov 17 '05 #3
That is correct. That is why they have two. ARE releases one thread and
resets automatically and MRE releases all of them and you "manually" decide
when to reset. The one you pick depends on what your doing in the
application.

--
William Stacey [MVP]

"Elhurzen" <66*******@sneakemail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

I've noticed an error in my original question after checking with MSDN
docs.

MSDN on ManualResetEvent:
"Threads that call WaitOne on the ManualResetEvent will block, awaiting
the signal. When the controlling thread completes the activity, it
calls Set to signal that the waiting threads can proceed. *** All
waiting threads are released. ***"

But for AutoResetEvent, MSDN says:
"Calling Set signals AutoResetEvent to release a waiting thread.
AutoResetEvent remains signaled *** until a single waiting thread is
released ***, and then automatically returns to the nonsignaled state.
If no threads are waiting, the state remains signaled indefinitely."

I read the AutoResetEvent description and assumed that the same would
apply to ManualResetEvent.

But ManualResetEvent wakes up ALL waiting threads while AutoResetEvent
might move on after waking up just one. I haven't actually been able to
try this, but please correct me if I've understood this incorrectly.

Why is there such a discrepancy? Why can't AutoResetEvent wake up all
of its own waiting threads?

In any case, my "problem" with ManualResetEvent seems to be solved. :-)

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Guyon Morée | last post: by
10 posts views Thread by MikeE | last post: by
6 posts views Thread by Quiet Man | last post: by
1 post views Thread by Raoul Minder | last post: by
2 posts views Thread by Tumurbaatar S. | last post: by
2 posts views Thread by Stressed Out Developer | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.