监视器和信号量之间的主要区别是什么?
当前回答
下面的解释实际上解释了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.
链接:此处可进一步阅读。希望能有所帮助。
其他回答
信号量允许多个线程(最多设置一个数目)访问一个共享对象。监视器允许对共享对象的互斥访问。
监控
信号量
一句话回答:
监视器:一次只能控制一个线程在监视器中执行。(需要获取锁才能执行单线程)
信号量:保护共享资源的锁。(需要获取锁才能访问资源)
Monitor是设计用于从多个线程访问的对象。监视对象的成员函数或方法将强制互斥,因此在给定时间只有一个线程可以对对象执行任何操作。如果一个线程正在执行该对象的成员函数,那么任何试图调用该对象的成员函数的其他线程都必须等待,直到第一个线程完成。
信号量是较低级别的对象。您可以使用信号量来实现监视器。信号量本质上就是一个计数器。当计数器为正数时,如果线程试图获取信号量,则允许获取信号量,计数器则递减。当一个线程完成时,它释放信号量,并增加计数器。
如果当一个线程试图获取信号量时计数器已经为零,那么它必须等待另一个线程释放信号量。如果一个线程释放一个信号量时有多个线程在等待,那么其中一个线程会得到它。释放信号量的线程不一定是获取信号量的线程。
显示器就像一个公共厕所。每次只能有一个人进入。他们锁上门防止其他人进来,做他们的事情,然后在他们离开的时候打开门。
信号量就像一个自行车租赁场所。他们有一定数量的自行车。如果你想租一辆自行车,他们有一辆免费的,那么你可以骑,否则你必须等待。当有人归还自行车时,其他人就可以拿走它。如果你有一辆自行车,那么你可以把它交给别人归还——自行车租赁公司不在乎谁来归还,只要他们能把自行车拿回来就行。
下面的解释实际上解释了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.
链接:此处可进一步阅读。希望能有所帮助。
当一个信号量被用来保护一个关键区域时,信号量和被保护的数据之间没有直接的关系。这就是为什么信号量可能分散在代码中,以及为什么很容易忘记调用wait或notify的部分原因,在这种情况下,结果分别是违反互斥或永久锁定资源。
相比之下,这些不好的事情都可能发生在显示器上。监控器直接访问数据(它封装数据),由于监控器操作是原子动作,因此不可能编写不调用入口协议就可以访问数据的代码。退出协议在监视器操作完成时自动调用。
监控器有一个内置的机制,可以在继续之前以条件变量的形式进行条件同步。如果条件不满足,则流程必须等待,直到收到条件更改的通知。当一个进程等待条件同步时,监视器实现会处理互斥问题,并允许另一个进程访问该监视器。
选自开放大学M362第三单元“互动过程”课程材料。
推荐文章
- Thread start()和Runnable run()有什么区别
- 如何在Python中获取线程id ?
- 一个Java虚拟机可以支持多少线程?
- 到底什么是std::atomic?
- RxJava调度器用例
- 获取当前在Java中运行的所有线程的列表
- c#静态构造函数线程安全吗?
- CompletableFuture, Future和RxJava的Observable之间的区别
- 信号量和监视器——有什么不同?
- 同时运行多个asynctask——不可能?
- java.lang.Thread.interrupt()做什么?
- 在调用线程中捕获线程的异常?
- 如何超时线程
- Linux中的线程与进程
- join()在线程中的用途是什么?