监视器与互斥体 [英] Monitor vs Mutex

查看:19
本文介绍了监视器与互斥体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到 mutex 是一个值为 1 的信号量(二进制信号量),用于强制互斥.

I read that mutex is a semaphore with value 1 (binary semaphore) used to enforce mutual exclusion.

我读了这个链接信号量与监视器 - 有什么区别?这表示监视器有助于实现互斥.

I read this link Semaphore vs. Monitors - what's the difference? which says that monitor helps in achieving mutual exclusion.

谁能告诉我互斥锁和监视器之间的区别,因为它们都有助于实现相同的目标(互斥)?

Can someone tell me the difference between mutex and monitor as both help achieve the same thing (Mutual Exclusion)?

推荐答案

由于你没有具体说明你说的是哪个操作系统或语言/库,所以让我笼统地回答一下.

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

监控

通常,监视器的实现更快/更轻,因为它是为同一进程内的多线程同步而设计的.此外,它通常由框架/库本身提供(而不是请求操作系统).

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).

互斥体

通常,互斥体由操作系统内核提供,库/框架只是提供一个接口来调用它.这使得它们重量级/速度更慢,但它们在不同进程的线程中工作.操作系统还可能提供按名称访问互斥锁的功能,以便在单独的可执行文件的实例之间轻松共享(而不是使用只能由 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).

这篇关于监视器与互斥体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆