multithreading 信号量与监视器 - 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7335950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Semaphore vs. Monitors - what's the difference?
提问by user919860
What are the major differences between a Monitorand a Semaphore?
监视器和信号量之间的主要区别是什么?
回答by Anthony Williams
A Monitoris an object designed to be accessed from multiple threads. The member functions or methods of a monitor object will enforce mutual exclusion, so only one thread may be performing any action on the object at a given time. If one thread is currently executing a member function of the object then any other thread that tries to call a member function of that object will have to wait until the first has finished.
甲监视器是设计成从多个线程访问的对象。监视器对象的成员函数或方法将强制互斥,因此在给定时间只有一个线程可能对对象执行任何操作。如果一个线程当前正在执行对象的成员函数,则任何其他试图调用该对象的成员函数的线程都必须等到第一个线程完成。
A Semaphoreis a lower-level object. You might well use a semaphore to implement a monitor. A semaphore essentially is just a counter. When the counter is positive, if a thread tries to acquire the semaphore then it is allowed, and the counter is decremented. When a thread is done then it releases the semaphore, and increments the counter.
甲信号量是一种较低级的对象。您可能会使用信号量来实现监视器。信号量本质上只是一个计数器。当计数器为正时,如果线程尝试获取信号量,则允许它,并且计数器递减。当一个线程完成时,它会释放信号量,并增加计数器。
If the counter is already zero when a thread tries to acquire the semaphore then it has to wait until another thread releases the semaphore. If multiple threads are waiting when a thread releases a semaphore then one of them gets it. The thread that releases a semaphore need not be the same thread that acquired it.
如果当一个线程尝试获取信号量时计数器已经为零,那么它必须等到另一个线程释放信号量。如果多个线程在一个线程释放信号量时正在等待,则其中一个获得它。释放信号量的线程不必与获取它的线程相同。
A monitor is like a public toilet. Only one person can enter at a time. They lock the door to prevent anyone else coming in, do their stuff, and then unlock it when they leave.
监视器就像一个公共厕所。一次只能有一个人进入。他们锁上门以防止其他人进来,做他们的事情,然后在他们离开时解锁。
A semaphore is like a bike hire place. They have a certain number of bikes. If you try and hire a bike and they have one free then you can take it, otherwise you must wait. When someone returns their bike then someone else can take it. If you have a bike then you can give it to someone else to return --- the bike hire place doesn't care who returns it, as long as they get their bike back.
信号量就像一个自行车出租场所。他们有一定数量的自行车。如果您尝试租一辆自行车并且他们免费提供一辆,那么您可以使用它,否则您必须等待。当有人归还他们的自行车时,其他人就可以取走它。如果您有自行车,那么您可以将其交给其他人归还——自行车租赁处不关心谁归还自行车,只要他们将自行车归还即可。
回答by Abu Shumon
Following explanation actually explains how wait() and signal() of monitor differ from P and V of semaphore.
下面的解释实际上解释了监视器的wait() 和signal() 与信号量的P 和V 有何不同。
The wait()and signal()operations on condition variables in a monitorare similar to Pand Voperations on counting semaphores.
监视器中条件变量的wait()和signal()操作类似于计数信号量的P和V操作。
A wait statementcan block a process's execution, while a signal statementcan cause another process to be unblocked. However, there are some differencesbetween 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 语句可以阻塞一个进程的执行,而一个signal 语句可以导致另一个进程被解除阻塞。但是,有一些差异它们之间。当进程执行 P 操作时,它不一定会阻塞该进程,因为计数信号量可能大于零。相反,当一个wait语句被执行时,它总是阻塞进程。当任务对信号量执行 V 操作时,它要么解除阻塞等待该信号量的任务,要么在没有要解锁的任务时增加信号量计数器。另一方面,如果一个进程在没有其他进程要解除阻塞时执行信号语句,则对条件变量没有影响。信号量和监视器之间的另一个区别是,被 V 操作唤醒的用户可以立即恢复执行。相反,被信号操作唤醒的用户只有在显示器解锁时才会重新启动。此外,
Link: herefor further reading. Hope it helps.
链接:这里为进一步阅读。希望能帮助到你。
回答by Billz
One Line Answer:
一行回复:
Monitor:controls only ONE thread at a time can execute in the monitor. (need to acquire lock to execute the single thread)
监视器:一次只能控制一个线程可以在监视器中执行。(需要获取锁才能执行单线程)
Semaphore:a lock that protects a shared resource. (need to acquire the lock to access resource)
信号量:保护共享资源的锁。(需要获取锁才能访问资源)
回答by tafoo85
回答by e.doroskevic
When a semaphore is used to guard a critical region, there is no direct relationship between the semaphore and the data being protected. This is part of the reason why semaphores may be dispersed around the code, and why it is easy to forget to call waitor notify, in which case the result will be, respectively, to violate mutual exclusion or to lock the resource permanently.
当信号量用于保护临界区时,信号量和被保护的数据之间没有直接关系。这就是信号量可能分散在代码周围的部分原因,以及为什么很容易忘记调用wait或notify,在这种情况下,结果将分别违反互斥或永久锁定资源。
In contrast, niehter of these bad things can happen with a monitor. A monitor is tired directly to the data (it encapsulates the data) and, because the monitor operations are atomic actions, it is impossible to write code that can access the data without calling the entry protocol. The exit protocol is called automatically when the monitor operation is completed.
相比之下,显示器几乎不会发生这些坏事。监视器对数据直接厌倦(它封装了数据),并且由于监视器操作是原子操作,因此无法编写可以在不调用入口协议的情况下访问数据的代码。当监控操作完成时,自动调用退出协议。
A monitor has a built-in mechanism for condition synchronisation in the form of condition variable before proceeding. If the condition is not satisfied, the process has to wait until it is notified of a change in the condition. When a process is waiting for condition synchronisation, the monitor implementation takes care of the mutual exclusion issue, and allows another process to gain access to the monitor.
监视器具有在继续之前以条件变量的形式进行条件同步的内置机制。如果不满足条件,则进程必须等待,直到收到条件更改的通知。当一个进程正在等待条件同步时,监视器实现会处理互斥问题,并允许另一个进程访问监视器。
Taken from The Open University M362 Unit 3 "Interacting process" course material.
取自开放大学 M362 第 3 单元“交互过程”课程材料。
回答by Barun
Semaphore :
信号量:
Using a counter or flag to control access some shared resources in a concurrent system, implies use of Semaphore.
使用计数器或标志来控制对并发系统中某些共享资源的访问,意味着使用Semaphore。
Example:
例子:
- A counter to allow only 50 Passengers to acquire the 50 seats (Shared resource) of any Theatre/Bus/Train/Fun ride/Classroom. And to allow a new Passenger only if someone vacates a seat.
- A binary flag indicating the free/occupied status of any Bathroom.
- Traffic lights are good example of flags. They control flow by regulating passage of vehicles on Roads (Shared resource)
- 一个柜台,只允许 50 名乘客获得任何剧院/公共汽车/火车/游乐设施/教室的 50 个座位(共享资源)。并且只有在有人腾出座位时才允许新乘客。
- 指示任何浴室的空闲/占用状态的二进制标志。
- 交通灯是旗帜的好例子。他们通过调节道路上的车辆通行来控制流量(共享资源)
Flags only reveal the current state of Resource, no count or any other information on the waiting or running objects on the resource.
标志仅显示资源的当前状态,不显示资源上正在等待或运行的对象的计数或任何其他信息。
Monitor :
监视器 :
A Monitorsynchronizes access to an Object by communicating with threads interested in the object, asking them to acquire access or wait for some condition to become true.
一个监视器与感兴趣的是对象的线程沟通,要求他们获取访问或等待一些条件,以成为真正的同步访问对象。
Example:
例子:
- A Father may acts as a monitor for her daughter, allowing her to date only one guy at a time.
- A school teacher using baton to allow only one child to speak in the class.
- Lastly a technical one, transactions (via threads) on an Account object synchronized to maintain integrity.
- 父亲可以充当她女儿的监护人,允许她一次只和一个男人约会。
- 一位学校老师使用警棍,只允许一个孩子在课堂上发言。
- 最后一个技术问题,同步帐户对象上的事务(通过线程)以保持完整性。