概述
先说结论:
在semaphore计数大于1的时候,关键不同点在于mutexes用于保护共享资源,而semaphorse用于线程间通信。semaphore用来保护几个相同的资源(资源相同并且只能被独占)是错误的用法。
在semaphore计数等于1的时候,关键不同点在于semaphore可以由其它线程释放,而mutexes只能由申请它的线程释放。
下面是对 http://stackoverflow.com/questions/34519/what-is-a-semaphore 第二名回答的翻译,摘抄了重要部份。
The article Mutexesand Semaphores Demystified by Michael Barr is a great short introductioninto what makes mutexes and semaphores different, and when they should andshould not be used. I've excerpted several key paragraphs here.
The key point is that mutexes should be used to protectshared resources, while semaphores should be used for signaling. You shouldgenerally not use semaphores to protect shared resources, nor mutexes for signaling.There are issues, for instance, with the bouncer analogy in terms of usingsemaphores to protect shared resources - you can use them that way, but it maycause hard to diagnose bugs.
Michael Barr文章Mutexesand Semaphores Demystified 是一遍很好的简介介绍mutexes和semaphores的不同和他们适用和不适用的时候。关键点在于mutexes用于保护共享资源,而semaphorse用于线程间通信。你不应该用semaphores来保护共享资源,反之亦然。举个例子,你可以使用semaphores来模拟保镖保护共享资源,你可以这么做,但会造成诊断bug的困难。
At this point an interesting analogy is made using the ideaof bathroom keys as protecting shared resources - the bathroom. If a shop has asingle bathroom, then a single key will be sufficient to protect that resourceand prevent multiple people from using it simultaneously.
If there are multiple bathrooms, one might be tempted tokey them alike and make multiple keys - this is similar to a semaphore beingmis-used. Once you have a key you don't actually know which bathroom isavailable, and if you go down this path you're probably going to end up usingmutexes to provide that information and make sure you don't take a bathroomthat's already occupied.
A semaphore is the wrong tool to protect several of theessentially same resource, but this is how many people think of it and use it.The bouncer analogy is distinctly different - there aren't several of the sametype of resource, instead there is one resource which can accept multiplesimultaneous users. I suppose a semaphore can be used in such situations, butrarely are there real-world situations where the analogy actually holds - it'smore often that there are several of the same type, but still individualresources, like the bathrooms, which cannot be used this way.
在这个地方可以打一个有趣的比方—用澡房的钥匙保护共享资源(澡房)。如果一个澡堂只有一间单独的澡房。一支钥匙来保护资源和阻止多个人同时使用它是有必要的。当有几个澡房呢。可能又人会为它们配置相同的几把钥匙,这和semahore被错误使用是很相似的。一旦你有了钥匙,你不一定确切知道哪一间澡房可以使用,因为每间澡房的钥匙都一样,你只需要来到澡房门前找一间没人的澡房进去即可。
Semaphore用来保护几个相同的资源是错误的用法,但人们往往在这种情况下想到它并使用。但保镖的比方完全不一样—资源类型可以不同,并且一个资源可以同时被多个用户使用。我认为semaphore能够用在这种情况下。但在现实生活中这种情况很少。更多情况下是拥有几个同种并且只能被独占的资源。
Here an important point is made that mutexes interfere withreal time operating systems in a bad way, causing priority inversion where aless important task may be executed before a more important task because ofresource sharing. In short, this happens when a lower priority task uses amutex to grab a resource, A, then tries to grab B, but is paused because B isunavailable. While it's waiting, a higher priority task comes along and needsA, but it's already tied up, and by a process that isn't even running becauseit's waiting for B. There are many ways to resolve this, but it most often isfixed by altering the mutex and task manager. The mutex is much more complex inthese cases than a binary semaphore, and using a semaphore in such an instancewill cause priority inversions because the task manager is unaware of thepriority inversion and cannot act to correct it.
这里还有一个重点导致使用mutexes在现实操作系统中是一个坏的尝试,因为如果一个低优先级的任务在高优先级的任务之前执行,并且它们共享资源,那么可能导致优先级反转。
简单来说。如果一个低优先级的任务使用mutex来独占资源A,然后想要获取b,但此时可能被堵塞因为b不能获取。在它等待的时候一个高优先级的任务来到并且需要获得A,但A已经被获取,并且获取的线程甚至不在running的状态因为它此时在尝试获取b中堵塞了。这时有几个方法来解决。但最常用的是替换mutex和添加任务管理器。这这种情况下使用mutex比semaphore更加复杂,在这种情况下只是 使用semaphore还不行,还必需要有一个任务调度器来意识到优先级反转然后主动来修复它,这里指semaphore强制release掉 。
最后的最后,经常出现的一个需求:
用来保护几个相同的资源(资源相同并且只能被独占)如何实现?
本人用java,暂时想到用concurrentHashMap来存储资源被使用的状态,在获取资源前先查询资源被使用状态,如果被使用就进入自旋或者挂起。
最后
以上就是虚拟煎蛋为你收集整理的semaphore与mutexes的区别的全部内容,希望文章能够帮你解决semaphore与mutexes的区别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复