简体   繁体   English

ManualResetEvent(EventWaitHandle)Set花费的CPU比WaitOne(超时)大得多

[英]ManualResetEvent(EventWaitHandle) Set spends much cpu than WaitOne(timeout)

While implementing ManualResetEvent something surprised me, 在实现ManualResetEvent时,我感到有些惊讶,

As far as I understand mre.Set() command signals and let other processes to execute. 据我了解mre.Set()命令信号并让其他进程执行。

mre.WaitOne(); Holds on the current line and waits for a signal. 保持当前行并等待信号。 Beside this if we use it with timeout mre.WaitOne(100ms); 除此之外,如果我们将其与超时mre.WaitOne(100ms);

BUT! 但! Lets suppose that StartCommunicate is a thread's job. 让我们假设StartCommunicate是线程的工作。

If I use waitHandle.Set(); 如果我使用waitHandle.Set(); my process uses ~%25 or for another project ~%1 CPU resource. 我的进程使用〜%25或用于另一个项目〜%1 CPU资源。

But if I use waitHandle.WaitOne(100); 但是如果我使用waitHandle.WaitOne(100); (the timeout value is symbolic. It (try)waits for a signal for 100ms). (超时值是符号值。它(尝试)等待100ms的信号)。

The process starts using ~%0 CPU resource with waitone(timeout) What does it mean ? 该进程开始使用带有Waitone(timeout)的 〜%0 CPU资源,这是什么意思? ThereIsAJobToExecute is Socket.HasData for me. ThereIsAJobToExecuteSocket.HasData我。 So does it mean that hitting much to SerialPort.BytesToRead or Socket.Available makes our CPU usage higher ? 那么这是否意味着对SerialPort.BytesToReadSocket.Available的访问过多会使我们的CPU使用率更高?

Does it any side effect for me holding the thread for 100 ms for every hit? 每次击中线程保持100毫秒对我有副作用吗? Supposing that a socket program or a rs232 connection baud rate is very low comparatively new generation PCs. 相对于新一代PC,假设套接字程序或rs232连接波特率非常低。

So using mre.WaitOne(1); 因此,使用mre.WaitOne(1); seems more preferable to me. 对我来说似乎更可取。 What do you think about it ? 你怎么看待这件事 ? I'm doing some experiments with some memory and performance profilers but I'm not sure if I'm doing optimum solution for various kind of client machines or not... 我正在使用一些内存和性能分析器进行一些实验,但不确定是否针对各种客户端计算机进行了最佳的解决方案...

Longing for your comments. 渴望您的评论。

Thanks in advance! 提前致谢!

    ManualResetEvent waitHandle = new ManualResetEvent(false);
    public void StartCommunicate()
    {
        while (true)
        {
            if (ThereIsAJobToExecute)
            {
                Execute the job here!
            }
            else {
                //waitHandle.Set();
                waitHandle.WaitOne(1);
            }                              
        }

    }

EDIT: For Socket programming it is available to work ASYN so we can easily do it by the below code and we don't need polling. 编辑:对于套接字编程,可以使用ASYN进行工作,因此我们可以通过以下代码轻松进行此操作,并且不需要轮询。

But RS232 COMM port programming I need it. 但是我需要RS232 COMM端口编程。 Or not ? 或不 ?

 do
 {
      socket.BeginReceiveASYN(....ReceiveCallBack,...,socket)
      mre.WaitOne();
      mre.Reset();
 }while(true)

     void ReceiveCallBack(IResult rst)
     {
     //get the socket and do my job here!
      mre.Set();
     }

WaitOne puts the thread in a suspended state, which does not cost CPU resources. WaitOne将线程置于挂起状态,这不会WaitOne CPU资源。 The signal from the ManualResetEvent later awakens the thread. 稍后,来自ManualResetEvent的信号将唤醒线程。

It's not 100% clear to me what you're using the ManualResetEvent for. 对我来说,您要使用ManualResetEvent的目的还不是100%清楚。 However... 然而...

Doing something like waitHandle.WaitOne(1) is pretty much pointless, as you're sleeping for such a small amount of time that you're effectivly busy waiting on that thread, and consuming CPU resources that are not doing anything. waitHandle.WaitOne(1)这样的事情几乎是没有意义的,因为您睡眠时间非常短,以至于您实际上在忙于等待该线程,并且消耗了没有做任何事情的CPU资源。

If you're looking to tell your thread that it should wake up and process data then try something like this: 如果您想告诉线程应该唤醒并处理数据,请尝试执行以下操作:

while(true)
{
  waitHandle.Wait();
  waitHandle.Reset();

  while(ThereIsAJobToExecute)
  {
    // Process the jobs
  }
}

This will put your thread to sleep when there is nothing to do, and it won't waste any resources. 当无事可做时,这会使您的线程进入睡眠状态,并且不会浪费任何资源。 Now you can signal it with waitHandle.Set() when there is work to do. 现在,您可以在有工作要做时用waitHandle.Set()发出信号。

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

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