Will ManualResetEvent consume the processor while it is idle?

In particular, is context switching performance degrading for idle threads?

Under what conditions or circumstances may you need ManualResetEvent or WaitHandle resources?

+4
source share
2 answers

ManualResetEvent does not have a wait state. The only thing that MRE can wait for is a thread. And yes, a thread consumes a lot of precious resources without the need, when it does not do what it had to do, to execute the code. A megabyte of virtual memory and several kernel objects. One core object that MRE consumes is a small potato compared to this.

Instead, you usually want to use threadpool thread.

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

+5
source

In the case of ManualResetEvent, no. The thread doesn't really loop or anything else. He just got a link to himself, filled in the ManualResetEvent notification list. When a thread ANOTHER calls. Set to ManualResetEvent, this other thread ends up returning the waiting thread to the active queue.

Consumable resources are just accounting for the existence of a thread: a stack, any kernel resources are written, registers are saved, etc. Now, if the thread you were talking about did not use ManualResetEvent, but instead has some sort of wait-loop, then I’m sure.

Now WaitHandle is not an implementation. This is just an abstract API. It is not known how other WaitHandle implementations may work.

+2
source

Source: https://habr.com/ru/post/1397221/


All Articles