二进制信号量和互斥量之间有区别吗?或者它们本质上是相同的?
当前回答
神话:
一些文章说“二进制信号量和互斥量是相同的”或“值为1的信号量是互斥量”,但基本的区别是互斥量只能由获得它的线程释放,而你可以从任何其他线程发出信号量
重点:
一个线程可以获得多个锁(互斥锁)。
只有递归互斥锁才能被锁多次,这里的锁和锁应该是一样的
•如果一个线程已经锁定了一个互斥锁,试图再次锁定互斥锁,它将进入该互斥锁的等待列表,这将导致死锁。
二进制信号量和互斥量相似但不相同。
互斥是昂贵的操作,因为与它相关的保护协议。
互斥的主要目的是实现对资源的原子访问或锁定
其他回答
在Windows上,互斥量和二进制信号量之间有两个区别:
互斥锁只能由拥有所有权的线程释放,即之前调用Wait函数的线程(或在创建互斥锁时获得所有权的线程)。任何线程都可以释放信号量。 线程可以在互斥锁上重复调用等待函数而不会阻塞。但是,如果你在一个二进制信号量上调用了两次等待函数,而中间没有释放信号量,线程就会阻塞。
Mutex is used to protect the sensitive code and data, semaphore is used to synchronization.You also can have practical use with protect the sensitive code, but there might be a risk that release the protection by the other thread by operation V.So The main difference between bi-semaphore and mutex is the ownership.For instance by toilet , Mutex is like that one can enter the toilet and lock the door, no one else can enter until the man get out, bi-semaphore is like that one can enter the toilet and lock the door, but someone else could enter by asking the administrator to open the door, it's ridiculous.
http://www.geeksforgeeks.org/archives/9102将详细讨论。
互斥是一种锁机制,用于同步对资源的访问。 信号量是一种信号机制。
如果他/她想使用二进制信号量来代替互斥量,这取决于程序员。
厕所的例子是一个有趣的类比:
Mutex: Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue. Officially: "Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section." Ref: Symbian Developer Library (A mutex is really a semaphore with value 1.) Semaphore: Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue. Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)." Ref: Symbian Developer Library
以上几乎所有人都说对了。如果有人还有疑问,让我来澄清一下。
互斥->用于序列化 信号- >同步。
两者的目的是不同的,但是,通过精心的编程,可以实现相同的功能。
标准示例->生产者消费者问题。
initial value of SemaVar=0
Producer Consumer
--- SemaWait()->decrement SemaVar
produce data
---
SemaSignal SemaVar or SemaVar++ --->consumer unblocks as SemVar is 1 now.
希望我能澄清。
推荐文章
- Trap和中断的区别是什么?
- 互斥实例/教程?
- 为什么Linux被称为单片内核?
- 如何检查Python的操作系统?
- 在Swift中,什么相当于Objective-C的“@synchronized”?
- 信号量和监视器——有什么不同?
- 如何使用JavaScript找到操作系统的详细信息?
- 我如何检查操作系统与预处理器指令?
- 如何在没有操作系统的情况下运行程序?
- 线程之间共享哪些资源?
- Windows、Mac OS X和Linux是用什么语言编写的?
- 什么时候应该使用自旋锁而不是互斥锁?
- context . start前台服务()没有调用service . start前台()
- 在c#中使用全局互斥锁的好模式是什么?
- 什么是信号量?