4

More specifically, does the performance degradation of context switching apply to threads that are in a wait state?

Under what conditions or circumstances would a ManualResetEvent, or WaitHandle, be likely to consume resources?

Eric Dahlvang
  • 8,252
  • 4
  • 29
  • 50
  • Are you expecting an answer to this question to show up here or on your [other question](http://stackoverflow.com/questions/9343358/is-this-background-thread-queue-a-performant-implementation) that you posted today (which contains the same question)? – M.Babcock Feb 18 '12 at 18:42
  • 1
    We'll need a bit of a code sample here to understand in what situation this occurs. – JaredPar Feb 18 '12 at 18:42
  • 2
    Well, apparently that question was not acceptable, so I'm trying to do this right. – Eric Dahlvang Feb 18 '12 at 18:43
  • 1
    @JaredPar I posted the code on the previous question, but it was considered better posted on codereview.stackexchange.com. – Eric Dahlvang Feb 18 '12 at 18:44
  • 2
    @EricDahlvang posts should be independent / self sufficient items. You need to post complete samples on every question you ask. Otherwise people will end up closing them as "not enough data to answer". – JaredPar Feb 18 '12 at 18:46
  • @SonicSoul Gosh, I've been so frustrated with this today. I'm an avid silent user of stack overflow, and figured this was a valid question that I've had difficulty finding an answer to on my own. So, I post a question with code...am told to move to code review, then I try reposting the question without the code, and am told I need a code sample. I'm screwed either way!!! – Eric Dahlvang Feb 18 '12 at 18:50
  • @EricDahlvang the question seems pretty valid and straight forward now. Maybe add a little context does not have to be code. +1 – Lloyd Feb 18 '12 at 18:55

2 Answers2

6

A ManualResetEvent doesn't have a wait state. The only thing that can wait on an MRE is a thread. And yes, a thread consumes plenty of precious resources needlessly when it is not doing what it was made to do, execute code. A megabyte of virtual memory and a handful of kernel objects. The single kernel object that MRE consumes is small potatoes compared to that.

You typically want to use a threadpool thread instead.

And look at what's available in .NET 4.0. Like ManualResetEventSlim (not based on an OS object) and the Task class.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for the reference to ManualResetEventSlim. I wasn't even aware this existed. There are certainly instances where we would be better off spinning than waiting. – Eric Dahlvang Feb 19 '12 at 17:48
3

In the case of a ManualResetEvent, no. The thread isn't actually looping, or anything. It's just got a reference to itself stuffed into the ManualResetEvent's notification list. When ANOTHER thread calls .Set on the ManualResetEvent, that other thread ends up putting the waiting thread back into the active queue.

The resources consumed are simply the accounting for the existence of the thread: the stack, whatever kernel resources are recorded, saved registers, etc. Now, if the thread you were speaking of wasn't using a ManualResetEvent, but instead a wait-loop of some sort, then sure.

Now, WaitHandle isn't an implementation. It's the just abstract API. There's no telling how other implementations of WaitHandle might work.

Jerome Haltom
  • 1,670
  • 2
  • 17
  • 23