简体   繁体   English

ManualResetEvent在处于等待状态时会消耗cpu吗?

[英]Will the ManualResetEvent consume cpu while it is in a wait state?

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? 在什么条件或情况下,ManualResetEvent或WaitHandle可能会消耗资源?

A ManualResetEvent doesn't have a wait state. ManualResetEvent没有等待状态。 The only thing that can wait on an MRE is a thread . 唯一可以在MRE上等待的是一个线程 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. 与之相比,MRE消耗的单个内核对象是小土豆。

You typically want to use a threadpool thread instead. 您通常希望使用线程池线程。

And look at what's available in .NET 4.0. 看看.NET 4.0中可用的内容。 Like ManualResetEventSlim (not based on an OS object) and the Task class. 像ManualResetEventSlim(不基于OS对象)和Task类。

In the case of a ManualResetEvent, no. 在ManualResetEvent的情况下,没有。 The thread isn't actually looping, or anything. 线程实际上并不是循环,也不是任何东西。 It's just got a reference to itself stuffed into the ManualResetEvent's notification list. 它只是引用了自己填充到ManualResetEvent的通知列表中。 When ANOTHER thread calls .Set on the ManualResetEvent, that other thread ends up putting the waiting thread back into the active queue. 当另一个线程在ManualResetEvent上调用.Set时,另一个线程最终将等待的线程放回到活动队列中。

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. 消耗的资源只是对线程存在的计算:堆栈,记录的内核资源,保存的寄存器等等。现在,如果您所说的线程没有使用ManualResetEvent,而是使用等待循环某种,然后肯定。

Now, WaitHandle isn't an implementation. 现在,WaitHandle不是一个实现。 It's the just abstract API. 这是一个抽象的API。 There's no telling how other implementations of WaitHandle might work. 没有人知道WaitHandle的其他实现如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM