监控vs互斥 [英] Monitor vs Mutex
问题描述
我读到互斥锁是一个值为1的信号量(二进制信号量),用于强制互斥.
I read that mutex is a semaphore with value 1 (binary semaphore) which is used for enforcing mutual exclusion.
我阅读了此链接 信号量与监视器之间的区别是什么? 表示监控器有助于实现互斥.
I read this link Semaphore vs. Monitors - what's the difference? which says that monitor helps in achieving mutual exclusion.
谁能告诉我互斥锁和监视器之间的区别,因为两者实际上都在做同一件事
Can anyone tell me the difference between mutex and monitor as both are actually doing the same thing
推荐答案
由于您尚未指定要使用的操作系统或语言/库,因此让我以一种通用的方式回答.
Since you haven't specified which OS or language/library you are talking about, let me answer in a generic way.
从概念上讲,它们是相同的.但是通常它们的实现略有不同
Conceptually they are the same. But usually they are implemented slightly differently
监视器
通常,由于监视器是为同一进程内的多线程同步而设计的,因此监视器的实现速度更快/重量更轻.而且,通常,它是由框架/库本身提供的(与请求OS相对).
Usually, the implementation of monitors is faster/light-weight, since it is designed for multi-threaded synchronization within the same process. Also, usually, it is provided by a framework/library itself (as opposed to requesting the OS).
Mutex
通常,互斥锁由OS内核提供,并且库/框架仅提供调用它的接口.这使它们变重/变慢,但是它们跨不同进程的线程工作.操作系统可能还会提供按名称访问互斥锁的功能,以便在单独的可执行文件实例之间轻松共享(与使用只能由fork
使用的句柄相反).
Usually, mutexes are provided by the OS kernel and libraries/frameworks simply provide an interface to invoke it. This makes them heavy-weight/slower, but they work across threads on different processes. OS might also provide features to access the mutex by name for easy sharing between instances of separate executables (as opposed to using a handle that can be used by fork
only).
这篇关于监控vs互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!