监视器和信号量之间的主要区别是什么?


当前回答

信号量允许多个线程(最多设置一个数目)访问一个共享对象。监视器允许对共享对象的互斥访问。

监控

信号量

其他回答

一句话回答:

监视器:一次只能控制一个线程在监视器中执行。(需要获取锁才能执行单线程)

信号量:保护共享资源的锁。(需要获取锁才能访问资源)

信号量:

在并发系统中使用计数器或标志来控制对某些共享资源的访问,意味着使用信号量。

例子:

一个只允许50名乘客获得任何剧院/公共汽车/火车/游乐设施/教室的50个座位(共享资源)的柜台。只有在有人空出座位的情况下,才允许新乘客进入。 一个二进制标志,指示任何浴室的空闲/占用状态。 交通灯是旗帜的一个很好的例子。他们通过调节道路上车辆的通行来控制流量(共享资源)

标志只显示资源的当前状态,没有计数或资源上正在等待或运行的对象的任何其他信息。

监控:

Monitor通过与对对象感兴趣的线程通信来同步对对象的访问,要求它们获得访问权或等待某些条件变为真。

例子:

父亲可能会充当女儿的监工,只允许她一次和一个男人约会。 学校老师使用警棍,只允许一个孩子在课堂上发言。 最后一个技术问题是,同步Account对象上的事务(通过线程)以保持完整性。

A semaphore is a signaling mechanism used to coordinate between threads. Example: One thread is downloading files from the internet and another thread is analyzing the files. This is a classic producer/consumer scenario. The producer calls signal() on the semaphore when a file is downloaded. The consumer calls wait() on the same semaphore in order to be blocked until the signal indicates a file is ready. If the semaphore is already signaled when the consumer calls wait, the call does not block. Multiple threads can wait on a semaphore, but each signal will only unblock a single thread.

计数信号量跟踪信号的数量。例如,如果生产者连续发出三次信号,wait()可以被调用三次而不阻塞。二进制信号量不算数,只有“等待”和“有信号”两种状态。

互斥锁(互斥锁)是由单个线程拥有的锁。只有获得锁的线程才能再次释放它。其他试图获取锁的线程将被阻塞,直到当前所有者线程释放它。互斥锁本身并不锁任何东西——它实际上只是一个标志。但是代码可以检查互斥锁的所有权,以确保一次只有一个线程可以访问某个对象或资源。

A monitor is a higher-level construct which uses an underlying mutex lock to ensure thread-safe access to some object. Unfortunately the word "monitor" is used in a few different meanings depending on context and platform and context, but in Java for example, a monitor is a mutex lock which is implicitly associated with an object, and which can be invoked with the synchronized keyword. The synchronized keyword can be applied to a class, method or block and ensures only one thread can execute the code at a time.

当一个信号量被用来保护一个关键区域时,信号量和被保护的数据之间没有直接的关系。这就是为什么信号量可能分散在代码中,以及为什么很容易忘记调用wait或notify的部分原因,在这种情况下,结果分别是违反互斥或永久锁定资源。

相比之下,这些不好的事情都可能发生在显示器上。监控器直接访问数据(它封装数据),由于监控器操作是原子动作,因此不可能编写不调用入口协议就可以访问数据的代码。退出协议在监视器操作完成时自动调用。

监控器有一个内置的机制,可以在继续之前以条件变量的形式进行条件同步。如果条件不满足,则流程必须等待,直到收到条件更改的通知。当一个进程等待条件同步时,监视器实现会处理互斥问题,并允许另一个进程访问该监视器。

选自开放大学M362第三单元“互动过程”课程材料。

下面的解释实际上解释了monitor的wait()和signal()与semaphore的P和V的区别。

监控器中对条件变量的wait()和signal()操作类似于对计数信号量的P和V操作。

A wait statement can block a process's execution, while a signal statement can cause another process to be unblocked. However, there are some differences between them. When a process executes a P operation, it does not necessarily block that process because the counting semaphore may be greater than zero. In contrast, when a wait statement is executed, it always blocks the process. When a task executes a V operation on a semaphore, it either unblocks a task waiting on that semaphore or increments the semaphore counter if there is no task to unlock. On the other hand, if a process executes a signal statement when there is no other process to unblock, there is no effect on the condition variable. Another difference between semaphores and monitors is that users awaken by a V operation can resume execution without delay. Contrarily, users awaken by a signal operation are restarted only when the monitor is unlocked. In addition, a monitor solution is more structured than the one with semaphores because the data and procedures are encapsulated in a single module and that the mutual exclusion is provided automatically by the implementation.

链接:此处可进一步阅读。希望能有所帮助。