473,385 Members | 2,014 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,385 software developers and data experts.

creating ManualResetEvent in thread

Hi all,

I read somewhere "using kernel stuff in thread is not good.."

if ManualResetEvent object is created in thread but not actually used, will
it affect performance?

Bob
Sep 13 '07 #1
9 2948
Bob,

There are two separate issues here. The first is whether or not to use
kernel stuff in a thread. If by kernel stuff you mean things like
ManualResetEvent instances, then the first statement is wrong, as it is
perfectly fine to use a ManualResetEvent in threads. As a matter of fact,
they are usually used to deal with synchronizing threads.

As for whether or not it will affect your performance, what is the
similar mechanism that you are comparing it against?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"bbg" <bb*@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
Hi all,

I read somewhere "using kernel stuff in thread is not good.."

if ManualResetEvent object is created in thread but not actually used,
will
it affect performance?

Bob
Sep 13 '07 #2
bbg wrote:
I read somewhere "using kernel stuff in thread is not good.."
Where? Sounds like a misinterpretation at best, and a completely bogus
piece of advice at worst.
if ManualResetEvent object is created in thread but not actually used, will
it affect performance?
Just one? Almost certainly not. But if you're not going to use it, why
create it?
Sep 13 '07 #3
Hi Nicholas,

I am not interested in what other mechanism is available for synchronizing
threads. What I am curious is that just creating ManualReset object but not
using at all in thread will degrade performance..

Bob
"Nicholas Paldino [.NET/C# MVP]" wrote:
Bob,

There are two separate issues here. The first is whether or not to use
kernel stuff in a thread. If by kernel stuff you mean things like
ManualResetEvent instances, then the first statement is wrong, as it is
perfectly fine to use a ManualResetEvent in threads. As a matter of fact,
they are usually used to deal with synchronizing threads.

As for whether or not it will affect your performance, what is the
similar mechanism that you are comparing it against?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"bbg" <bb*@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
Hi all,

I read somewhere "using kernel stuff in thread is not good.."

if ManualResetEvent object is created in thread but not actually used,
will
it affect performance?

Bob
Sep 13 '07 #4
bbg wrote:
I am not interested in what other mechanism is available for synchronizing
threads.
Well, you should be, since it's very much related to your original post.

Whether you are interested or not, it _is_ an example of a specific
reason ManualResetEvents are used in a thread. It's an obvious
counter-example to the "using kernel stuff in thread is not good" you
mentioned, so it's a natural thing for Nicholas to bring up.

There's no need for you to point out that you're not interested. If
you're not interested, just ignore it and move on.
What I am curious is that just creating ManualReset object but not
using at all in thread will degrade performance..
Then you may want to consider answering the question Nicholas asked, so
that he can provide more specific advice.

And for what it's worth, there's no such thing as "using kernel stuff"
_not_ in a thread. All code executes in a thread. You may only have a
single thread in which your code executes, but there's still a thread.

Pete
Sep 13 '07 #5
I am working on window service application in which threads come and go
and found ManualResetEvent was created but not used(nobody waiting).
So I became curious how creating unnecessary ManualResetEvent will affect
performance.

About "using kernel stuff in thread is not good..",

http://msdn.microsoft.com/msdnmag/is...urrentAffairs/

Bob

"Peter Duniho" wrote:
bbg wrote:
I am not interested in what other mechanism is available for synchronizing
threads.

Well, you should be, since it's very much related to your original post.

Whether you are interested or not, it _is_ an example of a specific
reason ManualResetEvents are used in a thread. It's an obvious
counter-example to the "using kernel stuff in thread is not good" you
mentioned, so it's a natural thing for Nicholas to bring up.

There's no need for you to point out that you're not interested. If
you're not interested, just ignore it and move on.
What I am curious is that just creating ManualReset object but not
using at all in thread will degrade performance..

Then you may want to consider answering the question Nicholas asked, so
that he can provide more specific advice.

And for what it's worth, there's no such thing as "using kernel stuff"
_not_ in a thread. All code executes in a thread. You may only have a
single thread in which your code executes, but there's still a thread.

Pete
Sep 13 '07 #6
bbg wrote:
I am working on window service application in which threads come and go
and found ManualResetEvent was created but not used(nobody waiting).
So I became curious how creating unnecessary ManualResetEvent will affect
performance.
Well, IMHO the simplest way to find out is to test performance with and
without the object and see how it differs, if at all.

If all the code is doing is creating the event handle once and then not
doing anything with it, I doubt you'd notice any performance difference
at all.
About "using kernel stuff in thread is not good..",

http://msdn.microsoft.com/msdnmag/is...urrentAffairs/
I didn't see anything in that article that said "using kernel stuff in
thread is not good". If you think it does say that, perhaps you could
quote the exact passage, so that we know exactly which part of the
article gave you that impression.

What I did see is a comment regarding the expense of creating a
ManualResetEvent. The author also says that using one is expensive, but
I'd disagree with that. At the very least, using an event handle isn't
anything like creating an event handle, in terms of performance cost.

Certainly using an event handle is more expensive than not using one.
But then, synchronization in general can be expensive. On the other
hand, getting the wrong results from your code is usually worse, so
programmers opt for "expensive" over "incorrect". :)

Also, I'd have to go back and check, but my recollection is that, as is
the case with many synchronization objects, as long as there's no
contention the use of the object is actually quite efficient. It's only
when multiple threads are trying to get at the object simultaneously
that it gets expensive. But again, that's exactly the scenario in which
you really need the object, so the cost is worthwhile.

He is correct that you would probably want to avoid creating and
destroying event handles over and over if you can. But that's not the
same as the question of whether having a single event handle existing in
the thread would hurt performance, which is the question you seem to be
asking.

Pete
Sep 13 '07 #7
bbg wrote:
I am working on window service application in which threads come and go
and found ManualResetEvent was created but not used(nobody waiting).
So I became curious how creating unnecessary ManualResetEvent will affect
performance.

About "using kernel stuff in thread is not good..",

http://msdn.microsoft.com/msdnmag/is...urrentAffairs/
By the way, I went ahead and downloaded the code from that article so I
could try it myself. Unfortunately, Richter doesn't actually state the
specific results he got, however I was unable to reproduce a significant
difference in performance.

In fact, with the exact code available for download, while his
implementation is in fact faster for a task that doesn't actually do
anything, the built-in CLR implementation of the async processing is
_faster_ than his "don't create the event handle" version when the task
actually takes some time to complete.

On my PC, in the 0-time case for 100,000 task iterations, I get about
1.4 seconds for his implementation and 4.4 seconds for the CLR
implementation. His wins that round.

But when I change it so that the task length is 1 second, reducing the
iterations to 1000 (otherwise it takes too long to complete the test :)
), his version takes 47 seconds while the CLR takes only 40 seconds.

If I do 1000 iterations of 100 ms-long tasks (this required changing his
LongTask class so that I could initialize it using a TimeSpan instead of
with seconds, of course), then his implementation takes about 10.3
seconds while the CLR only takes 4.3 seconds.

Out of three different scenarios, his implementation wins only one,
while the CLR wins the other two. I suspect the CLR would win _any_
scenario in which the task thread doesn't complete immediately.

This is on a Core 2 Duo, 2.33Ghz computer running with Windows XP SP 2
and .NET 2.0.

As much as I respect what Richter writes elsewhere, I don't see any
strong evidence here that creating an event handle really impedes
performance. It seems to me that he hasn't really measured performance
in an interesting or useful way.

Even if creating an event handle does have some significant overhead
(and IMHO that's far from proven by the code for that article), whatever
difference exists is easily swamped by other performance differences
elsewhere in the code (for example, the context switching forced by
making the task threads block...and IMHO if you've got something that
costs less than a context switch, it's probably not worth worrying about
unless you've got a task that can complete in a single timeslice).

Pete
Sep 13 '07 #8
In my application, using his AsyncResultNoResult in some tasks - communicate
with database, create socket connection - resulted in about 30% speep up
compared to using BeginInvoke. The performance difference was not that big as
that of his example code but it was still fairly better than CLR. Can you
tell me what test you did?

Bob

"Peter Duniho" wrote:
bbg wrote:
I am working on window service application in which threads come and go
and found ManualResetEvent was created but not used(nobody waiting).
So I became curious how creating unnecessary ManualResetEvent will affect
performance.

About "using kernel stuff in thread is not good..",

http://msdn.microsoft.com/msdnmag/is...urrentAffairs/

By the way, I went ahead and downloaded the code from that article so I
could try it myself. Unfortunately, Richter doesn't actually state the
specific results he got, however I was unable to reproduce a significant
difference in performance.

In fact, with the exact code available for download, while his
implementation is in fact faster for a task that doesn't actually do
anything, the built-in CLR implementation of the async processing is
_faster_ than his "don't create the event handle" version when the task
actually takes some time to complete.

On my PC, in the 0-time case for 100,000 task iterations, I get about
1.4 seconds for his implementation and 4.4 seconds for the CLR
implementation. His wins that round.

But when I change it so that the task length is 1 second, reducing the
iterations to 1000 (otherwise it takes too long to complete the test :)
), his version takes 47 seconds while the CLR takes only 40 seconds.

If I do 1000 iterations of 100 ms-long tasks (this required changing his
LongTask class so that I could initialize it using a TimeSpan instead of
with seconds, of course), then his implementation takes about 10.3
seconds while the CLR only takes 4.3 seconds.

Out of three different scenarios, his implementation wins only one,
while the CLR wins the other two. I suspect the CLR would win _any_
scenario in which the task thread doesn't complete immediately.

This is on a Core 2 Duo, 2.33Ghz computer running with Windows XP SP 2
and .NET 2.0.

As much as I respect what Richter writes elsewhere, I don't see any
strong evidence here that creating an event handle really impedes
performance. It seems to me that he hasn't really measured performance
in an interesting or useful way.

Even if creating an event handle does have some significant overhead
(and IMHO that's far from proven by the code for that article), whatever
difference exists is easily swamped by other performance differences
elsewhere in the code (for example, the context switching forced by
making the task threads block...and IMHO if you've got something that
costs less than a context switch, it's probably not worth worrying about
unless you've got a task that can complete in a single timeslice).

Pete
Sep 13 '07 #9
bbg wrote:
In my application, using his AsyncResultNoResult in some tasks - communicate
with database, create socket connection - resulted in about 30% speep up
compared to using BeginInvoke. The performance difference was not that big as
that of his example code but it was still fairly better than CLR. Can you
tell me what test you did?
I don't understand your question. As I said, I simply downloaded the
code he published, compiled it and ran it.

For one of the tests I had to modify the class slightly so that I could
specify an under-one-second task duration, and of course for the two
tasks where the task has a non-zero duration I changed the iteration
count so that the test would complete in a reasonable amount of time.

I did not even bother to remove the functional test from his sample; I
didn't do anything that would affect it, but I just let it run anyway.

Basically, I changed nothing except the specific iteration count and
task duration used in the test. Otherwise, the code I ran was identical
to the code provided with the article.

Pete
Sep 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kovan Akrei | last post by:
Hi, I would love to know if it is possible some how to ensure that a thread T1 does not call T2.myResetEvent.Set() before T1 has blocked by calling T1.myResetEvent.WaitOne()? I cant use monitors...
1
by: Pujo Aji | last post by:
Hello, I use ManualResetEvent to stop and allow a thread running by setting: public static ManualResetEvent mre = new ManualResetEvent(false); So if I use mre.WaitOne(); this let the thread...
0
by: Mark | last post by:
Hi, How do I stop a ManualResetEvent.WaitOne from intermittently jumping to 0? I am using version 1.1 of the framework. My ManualResetEvent instance is named "bev". To make the WaitOne work...
3
by: Elhurzen | last post by:
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...
0
by: Tomasz Kaszuba | last post by:
Is it possible to use the ManualResetEvent in my ASP.NET application? When I try to use it Asp.Net ignores the call to ManualResetEvent.WaitOne() (which should hold the thread indefinetly) and...
2
by: Brian Mitchell | last post by:
I am sure this is pretty basic but I have never worked with ResetEvents before, but how do I signal a ManualResetEvent of a module that is spun off into several threads? For instance, if I have 5...
13
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I...
15
by: Peter Larsen [] | last post by:
Hi, I have a problem using a ManuelResetEvent in the GUI thread while receiving events from the Shell (new folders, rename etc). This is what i do: Receiving an event from the Shell....
7
by: =?Utf-8?B?cmJEZXZlbG9wZXI=?= | last post by:
The following is from a simple Windows application in VS2005, which has button1 and textbox1 dragged onto a form. In StartThreads(), I call ThreadPool.QueueUserWorkItem(), then call WaitOne()....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.