473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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 12569
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Guyon Morée | last post by:
If I have multiple threads reading from the same file, would that be a problem? if yes, how would I solve it? Let's say I want to take it a step further and start writing to 1 file form...
10
by: MikeE | last post by:
Hi all, What's the best way to queue up and wait for number of threads to complete. This problem was trivial in VC++ 6 but I'm finding it rather hard to solve in VB.NET. My calculations run...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
3
by: mphanke | last post by:
Hi, I would like to implement a windows service which forks a configurable number of threads which all siten to the same port and reply to requests of different clients. How would I implement...
1
by: Raoul Minder | last post by:
Hi all I am new to threading! I am developping a newsletter tool including a dispatch manager that should schedule sendings. The Web layer kicks a remote object hosted by a windows service...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
18
by: Zytan | last post by:
I have multiple threads writing to WebBrowser (using a function that checks InvokedRequired, and if so, invokes itself on the WebBrowser thread) and they are getting deadlocked. They only...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.