监视器和监视器之间的区别 锁?

发布于 2024-07-21 21:18:31 字数 285 浏览 4 评论 0原文

监视器锁定

如果锁只是互斥的实现,那么监视器只是利用方法执行之间的等待时间的一种方式吗?

一个好的解释会非常有帮助,谢谢......

问候

What's the difference between a monitor and a lock?

If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions?

A good explanation would be really helpful thanks....

regards

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

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

发布评论

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

评论(9

月亮是我掰弯的 2024-07-28 21:18:31

例如,在 C# .NET 中,锁定语句相当于:

Monitor.Enter(object);
try
{
    // Your code here...
}
finally
{
    Monitor.Exit(object);
}

但是,请记住,Monitor 还可以 Wait()Pulse(),这在复杂的情况下通常很有用。多线程情况。

编辑:
在.NET框架的更高版本中,这被更改为:

bool lockTaken = false;
try
{
    Monitor.Enter(object, ref lockTaken);
    // Your code here...
}
finally
{
    if (lockTaken)
    {
        Monitor.Exit(object);
    }
}

For example in C# .NET a lock statement is equivalent to:

Monitor.Enter(object);
try
{
    // Your code here...
}
finally
{
    Monitor.Exit(object);
}

However, keep in mind that Monitor can also Wait() and Pulse(), which are often useful in complex multithreading situations.

Edit:
In later versions of the .NET framework, this was changed to:

bool lockTaken = false;
try
{
    Monitor.Enter(object, ref lockTaken);
    // Your code here...
}
finally
{
    if (lockTaken)
    {
        Monitor.Exit(object);
    }
}
蓝戈者 2024-07-28 21:18:31

他们是相关的。 例如,在 C# 中,lock 语句是一个简单的 try-finally围绕输入监视器完成后退出

They're related. For example, in C# the lock statement is a simple try-finally wrapper around entering a Monitor and exiting one when done.

楠木可依 2024-07-28 21:18:31

监视器是编译器辅助的“半自动”锁。 它们允许在类上声明同步方法等。这只是提供互斥的一种不同方法。 我发现这本书是对这些概念最彻底的解释,尽管它主要面向操作系统开发人员。

Monitors are compiler-assisted "semi-automatic" locks. They allow one to declare synchronized methods on classes, etc. This is just a different approach to providing mutual exclusion. I found this book to be the most thorough explanation of the concepts, even though it's mostly geared towards OS developers.

苍景流年 2024-07-28 21:18:31

锁确保互斥。

监视器将要保护的数据与保护数据访问所需的互斥和同步原语关联起来。
例如,当您需要一个线程等待事件发生(例如,等待另一个线程将一项放入队列)时,就会使用同步。

A lock ensures mutual exclusion.

A monitor associates the data to be protected and the mutual exclusion and synchronization primitives required to protect accesses to the data.
Synchronization is used e.g. when you need one thread to wait until an event occurs (e.g., wait until another thread places an item in a queue).

有深☉意 2024-07-28 21:18:31

监视器是一种编程语言结构,其功能与半量/锁相同,但监视器通过在运行时同步来控制共享数据。 相比之下,锁仅通过“旋转”来保护共享数据,这可能会导致 CPU 利用率较低。

Monitors is a programming-language construct that does the same thing as semiphores/locks, but Monitors control the shared data by synchronizing at run time. In contrast, locks protect the shared data by just "spinning" which can lead to poor CPU utilization.

深府石板幽径 2024-07-28 21:18:31

没有区别,lock 在 try/finally 块内生成 Monitor.Enter 和 Monitor.Exit。 使用 Monitor over lock 可以让您进行微调,因为它具有 Pulse 和 PulseAll。 如果您无法使用 TryEnter 获取锁,您还可以进行替代处理。

There is no difference, lock generates Monitor.Enter and Monitor.Exit within a try/finally block. Using Monitor over lock allows you to fine tune because it has Pulse and PulseAll. You can also have alternate processing should you be unable to acquire the lock with TryEnter.

转身泪倾城 2024-07-28 21:18:31

Monitor是概念,Lock是实际实现。

Monitor is the concept and Lock is the actual implementation.

独行侠 2024-07-28 21:18:31

据我到目前为止的研究,监视器是一组线程同步的原理,而锁以及“线程协作”设施(例如等待和通知)是Java中监视器的实现方式。 因此,如果我们试图在这两个概念之间形成确切的关系,那么锁是监视器实现的一部分(另一个是等待和通知机制)。 如果我错了,请纠正我,但如果纠正非常具体,我将不胜感激。

As far as I have researched so far, monitor is a set of principles for thread synchronization, while locks are, along with "thread cooperation" facilities like wait and notify, the way monitors are implemented in Java. So effectively, if we try to form the exact relationship between the two notions, locks are one part of the implementation of monitors (the other being wait and notify mechanisms). Please correct me if I'm wrong, but I would really appreciate if the correction is very specific.

梦毁影碎の 2024-07-28 21:18:31

将焦点锁定在相互排斥上,但是
Monitor 自动提供互斥。

所以我们不需要担心在Monitor中使用互斥。
我们只需要在编程时关注同步,而不是 ME。

Moniter提供了更加系统化的编程方式。
因此,它是更先进的一种。

Lock focus on only mutual exculsion, but
Moniter provides mutual exclusion automatically.

So we don't need to worry of using mutual exclusion in Monitor.
Instead of ME, we need to consern of sycronzing only when we do programming.

Moniter provides more systematical way of programming.
It, therefor, is more advanced one.

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