为了使ManualResetEvent的和Thread.sleep之间做出选择() [英] To make a choice between ManualResetEvent or Thread.Sleep()

查看:354
本文介绍了为了使ManualResetEvent的和Thread.sleep之间做出选择()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道采纳...我专注于我的操作完成的操作策略,其中,但我也想保持性能问题一分太...有一个叫方法执行( )这必须等待(同步运行),直到操作完成。这个操作发生在另一个线程。有2种方式来实现同样的事情...

I am not sure which strategy to adopt...I am focusing on my operation getting completed, but I'd also like to keep performance issues to a min too...there is a method called Execute() which has to wait (run synchronously) until an operation completes. This operation happens on another thread. There are 2 ways to implement the same thing...

通过使用ManualResetEvent的

By using ManualResetEvent

void Execute()
{
    taskHandle = new ManualResetEvent(false);
    .
    .
    //delegate task to another thread
    .
    .
    taskHandle.WaitOne();
}





通过一个简单的结构,而

OR

By using a simple while construct

void Execute()
{
    .
    .
    //delegate task to another thread
    .
    .
    while (!JobCompleted)
        Thread.Sleep(1000);
}

这两种方法之一,我应该采取

...为什么?

Which one of the two approaches should I adopt...why?

编辑:

Q2。如果我只是有一个空的同时contruct? ?什么区别...

Q2. What if I just had an empty while contruct? Whats the difference...?

while(!JobCompleted);



编辑:(这是我收集的前)

(something I gathered before)

http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml - 该文章说manualresets是comparitively慢,因为他们走出去的托管代码和回...

http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml - This article says manualresets are comparitively slower because they go out of managed code and back in...

推荐答案

出于好奇为什么的ManualResetEvent ,而不是的AutoResetEvent ?无论哪种方式,去与OS原始过睡眠检查睡眠方式。

Out of curiosity, why ManualResetEvent and not AutoResetEvent? Either way, go with the OS primitive over a sleep-check-sleep approach.

您也可以使用显示器锁定(显式通过 Monitor.Enter Monitor.Exit ,或通过块),但该方法应基于你实际上在做什么;如果它是一个情景这里只有其中的一个的事情的,我需要独占访问,然后用显示器锁。如果是我需要等到的其他线程完成的比资源访问等原因,然后用的AutoResetEvent ManualResetEvent的

You could also use a Monitor lock (either explicitly through Monitor.Enter and Monitor.Exit, or through a lock block), but the approach should be based upon what you're actually doing; if it's a scenario of "there's only one of these things and I need exclusive access", then use a Monitor lock. If it's "I need to wait until the other thread finishes for reasons other than resource access", then use an AutoResetEvent or ManualResetEvent.

建议使用的Thread.join 是好的,如果(和仅当)

The suggestions to use Thread.Join are good if (and only if)


  1. 您可以访问其他对象

  2. 您不想执行,直到其他线程终止

  1. You have access to the other Thread object
  2. You don't want to execute until the other thread terminates.

如果两个是不正确的(你用不上,或者其他线程将不会终止,它只是一个信号全部清除),那么的Thread.join 是不可行的。

If either isn't true (you don't have access, or the other thread won't terminate, it will just signal an "all clear") then Thread.Join isn't viable.

最坏的选择是

而(!JobCompleted) ;

因为这将占用与变量的不必要检查的处理器没有在它们之间没有任何停顿。是的,它会阻止你的线程,直到操作完成,但你会最大程度的发挥CPU使用率(或至少是单核心的价值)。

As that will tie up the processor with needless checks of the variable without any pause in between them. Yes, it will block your thread until the operation completes, but you'll max out CPU usage (or at least a single core's worth).

这篇关于为了使ManualResetEvent的和Thread.sleep之间做出选择()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆