寻找监视器与信号量的良好类比/示例

发布于 2024-07-19 07:22:18 字数 184 浏览 9 评论 0原文

监视器应该解决并发环境中的信号量问题。
我正在寻找使用监视器与信号量的良好类比。

请使用信息进行类比:
4 个任务(任务 A、任务 B、任务 C、任务 D)
1 个变量 varX

每个任务都希望根据某个事件来操作 varX

A monitor is supposed to solve problems with semaphores in concurrent environments.
I'm looking for a good analogy using a monitor verses semaphore.

Please use information for the analogy:
4 tasks (TaskA, TaskB, TaskC, TaskD)
1 variable varX

Each Task wants to manipulate varX based on some event.

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

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

发布评论

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

评论(3

山色无中 2024-07-26 07:22:18

假设一群病人想要去看医生。

信号量的实现是,他们都站在办公室门外,只要有一个病人出来,他们都试图挤过去,一个人设法进去,剩下的人又得等待。

监控实施是,所有进来的患者都被送到候诊室,将确定某种顺序,当一名患者完成后,另一名患者将被送往医生处。

它们基本上是相同的东西,只是监视器比信号量更加结构化。

Lets say a bunch of patients wants to go see a doctor.

A semaphore implementation would be they all stand outside the door to the office, as soon as one patient comes out, they all try to squeeze through, one person manages to get in, the rest have to wait again.

A monitor implementation would be, all incoming patients are sent to a waiting room instead, some semblance of order will be determined and when one patient is done, another will be sent to the doctor.

They are basically the same thing, monitors are just more structured than semaphores.

药祭#氼 2024-07-26 07:22:18

您还可以将监视器视为公共厕所。 一旦有人进去并关上厕所门,里面的人就不希望其他人出现在那个空间(即监视器)。 所有其他人(线程)都得在厕所前面排队等待(wait())。 只有里面的人吃完了,它才出来,下一个人才能进去。

有些等待的人可能有便秘问题。 出于显而易见的原因,他们不想进去或回来,除非他们准备好正确使用厕所。 这是他们想要等待(wait())直到他们的胃向他们发出信号(signal())他们准备好去厕所的地方。 在此之前,他们让其他人通过。

来源:www.mijnadres.net/published/Monitor%20Object%20Pattern.pdf

You can also see a monitor as a public toilet. Once someone went in an closed the toilet door, the person inside wants no one else to be in that space (i.e. the monitor). All other people (threads) have to queue up in front of the toilet and wait (wait()). Only after the person inside has finished, it comes out and the next person can go in.

Some of the people waiting might have constipation problems. For obvious reasons, they don't want to go in or return unless they're ready to make proper use of the toilet. This is where they want to wait (wait()) until their stomach signals them (signal()) that they are ready to go to the toilet. Before this happens, they let everyone else pass.

Source: www.mijnadres.net/published/Monitor%20Object%20Pattern.pdf

洛阳烟雨空心柳 2024-07-26 07:22:18

将资源争用与事件通知分开非常重要。 监视器和信号量用于限制对共享资源的访问。 监视器基本上是一个信号量,其计数为 1。如果每个任务都想要访问单个 varX,那么您需要使用监视器(或信号量为 1)来保护它:

Monitor.Enter 
// do something with varX
Monitor.Exit

或者

Semaphore.Acquire
// do something with varX
Semaphore.Release

使用信号量,您显然可以设置共享资源允许的并发参与者数量。

缺口。

Its important to separate out the resource contention from the event notification. A Monitor and Semaphore are used to limit access to a shared resource. A monitor is basically a semaphore whose count is 1. If each of your tasks wants to get access to the single varX, then you need to protect it using your monitor (or sempahore of 1):

Monitor.Enter 
// do something with varX
Monitor.Exit

or

Semaphore.Acquire
// do something with varX
Semaphore.Release

With a Semaphore you can obviously set the number of allowed concurrenct participants to the shared resource.

Nick.

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