什么时候应该使用 WaitHandle 而不是锁

发布于 2024-11-04 03:04:31 字数 79 浏览 2 评论 0原文

C#中,什么时候我们应该使用WaitHandle而不是lock

In C#, when we should use WaitHandle instead of lock ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柠檬色的秋千 2024-11-11 03:04:32

最好的解释就在这两个链接中。不幸的是,您必须阅读一些内容才能获得您提出的问题类型的完整答案:

这还包含一个示例:
http://msdn.microsoft.com/en-us/library /system.threading.waithandle.aspx

http://msdn.microsoft .com/en-us/library/kad9xah9.aspx

The best explanation lies in these two links. Unfortunately you're going to have to do some reading to get a full answer to the type of question you asked:

This contains an example as well:
http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx

http://msdn.microsoft.com/en-us/library/kad9xah9.aspx

转角预定愛 2024-11-11 03:04:31

锁与 WaitHandle 有着根本的不同。例如,使用锁时,您可以这样写:

lock (SomeResource)
{
    // do stuff here
}

第一行表示“我想要对该资源进行独占访问”,并且将等待该资源可用(即没有其他人拥有该资源的锁)。然后代码运行,最后锁被释放,以便其他人可以使用它。

WaitHandle 用于等待某个事件的发生。当您编写时:

MyWaitHandle.WaitOne();

您的代码正在等待某个其他线程(可能在不同的进程中)向 WaitHandle 发出信号。它所发出的信号可以是任何东西。也许您正在等待另一个线程完成其工作并说“我现在完成了”,然后您的代码才能继续。

因此,为了回答您的问题,当您想要获得对资源的独占访问权限时,应该使用锁。当您想要收到某些事件的通知时,您应该使用 WaitHandle

A lock is fundamentally different from a WaitHandle. With a lock you write, for example:

lock (SomeResource)
{
    // do stuff here
}

The first line says, "I want exclusive access to that resource," and will wait until that resource is available (i.e. nobody else has a lock on it). Then the code runs and at the end the lock is released so that others can use it.

WaitHandle is used to wait for some event to take place. When you write:

MyWaitHandle.WaitOne();

Your code is waiting for some other thread (possibly in a different process) to signal that WaitHandle. What it signals could be anything. Perhaps you're waiting for another thread to finish its job and say, "I'm done now," before your code can continue.

So, to answer your question, you should use a lock when you want to gain exclusive access to a resource. You should use WaitHandle when you want to be notified of some event.

很酷不放纵 2024-11-11 03:04:31

当你需要的时候。说真的,lock是Monitor的语法糖,它可能在内部使用了WaitHandles。

仅当您需要更大的灵活性时才使用它(例如,持有锁的时间比当前范围长......)或以正确的顺序管理多个锁等。

When you need it. Seriously, lock is syntactic sugar with Monitor, which probably uses WaitHandles internally.

Only use it if you need more flexibility (like e.g. holding on to a lock longer than the current scope...) or manage multiple locks in the right order etc.

只是偏爱你 2024-11-11 03:04:31

他们完成完全不同的事情。 用于将一次对特定代码块的访问限制为单个线程。 WaitHandle 是几个与并发相关的类的基类,例如 SemaphoreMutexAutoResetEventManualResetEvent,它允许您以稍微不同的方式控制并发。例如,Semaphore 允许您将并发限制为不超过 N 个线程,而不是单个线程。

They accomplish completely different things. A lock is used to limit access to a specific block of code to a single thread at a time. A WaitHandle is the base class for several concurrency-related classes, such as Semaphore, Mutex, AutoResetEvent, and ManualResetEvent, which allow you to control concurrency in slightly different ways. For instance, a Semaphore allows you to limit concurrency to no more than N threads, instead of a single thread.

谁把谁当真 2024-11-11 03:04:31

一个 WaitHandle 可以等待多个句柄,而一个锁只能等待一个。

A WaitHandle can wait for multiple handles, a lock can wait for only one.

你的呼吸 2024-11-11 03:04:31

当您有多个线程正在运行并且您希望等待其中一个或所有线程完成后再继续执行某些其他操作时,通常会使用 WaitHandle。如果您想防止多个线程同时访问共享资源,则可以使用lock

WaitHandle is often used when you have multiple threads running and you want to wait for one or all of them to complete before you continue to do some other action. lock would be used if you want to prevent multiple threads from accessing a shared resource at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文