Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个。一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行。

Semaphore是一件可以容纳N人的房间,如果人不满就可以进去,如果人满了,就要等待有人出来。对于N=1的情况,称为binary semaphore。一般的用法是,用于限制对于某一资源的同时访问。

Binary semaphoreMutex的差异:

在 有的系统中Binary semaphore与Mutex是没有差异的。在有的系统上,主要的差异是mutex一定要由获得锁的进程来释放。而semaphore可以由其它进程释 放(这时的semaphore实际就是个原子的变量,大家可以加或减),因此semaphore可以用于进程间同步。Semaphore的同步功能是所有 系统都支持的,而Mutex能否由其他进程释放则未定,因此建议mutex只用于保护critical section。而semaphore则用于保护某变量,或者同步。

关于semaphore和mutex的区别,网上有著名的厕所理论(http://koti.mbnet.fi/niclasw/MutexSemaphore.html):

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

所以,mutex就是一个binary semaphore (值就是0或者1)。但是他们的区别又在哪里呢?主要有两个方面:

* 初始状态不一样:mutex的初始值是1(表示锁available),而semaphore的初始值是0(表示unsignaled的状态)。随后的操 作基本一样。mutex_lock和sem_post都把值从0变成1,mutex_unlock和sem_wait都把值从1变成0(如果值是零就等 待)。初始值决定了:虽然mutex_lock和sem_wait都是执行V操作,但是sem_wait将立刻将当前线程block住,直到有其他线程 post;mutex_lock在初始状态下是可以进入的。
    * 用法不一样(对称 vs. 非对称):这里说的是“用法”。Semaphore实现了signal,但是mutex也有signal(当一个线程lock后另外一个线程 unlock,lock住的线程将收到这个signal继续运行)。在mutex的使用中,模型是对称的。unlock的线程也要先lock。而 semaphore则是非对称的模型,对于一个semaphore,只有一方post,另外一方只wait。就拿上面的厕所理论来说,mutex是一个钥 匙不断重复的使用,传递在各个线程之间,而semaphore择是一方不断的制造钥匙,而供另外一方使用(另外一方不用归还)。

前面的实验证明,mutex确实能够做到post和wait的功能,只是大家不用而已,因为它是“mutex”不是semaphore。

下面给出一个例子:

要 让一个thread在背景不断的执行,最简单的方式就是在该thread执行无穷回圈,如while(1) {},这种写法虽可行,却会让CPU飙高到100%,因为CPU一直死死的等,其实比较好的方法是,背景平时在Sleep状态,当前景呼叫背景时,背景马 上被唤醒,执行该做的事,做完马上Sleep,等待前景呼叫。当背景sem_wait()时,就是马上处于Sleep状态,当前景sem_post() 时,会马上换起背景执行,如此就可避免CPU 100%的情形了。

/**//*(C) OOMusou 2006 http://oomusou.cnblogs.comFilename : pthread_create_semaphore.cppCompiler : gcc 4.10 on Fedora 5 / gcc 3.4 on Cygwin 1.5.21Description : Demo how to create thread with semaphorein Linux.Release : 12/03/2006Compile : g++-lpthread pthread_create_semaphore.cpp*/
#include <stdio.h> // printf(),
#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <pthread.h> // pthread_create(), pthread_join()
#include <semaphore.h> // sem_init()sem_t binSem;void* helloWorld(void* arg);int main() {// Result for System callint res = 0;// Initialize semaphoreres = sem_init(&binSem, 0, 0);if (res) {printf("Semaphore initialization failed!!/n");exit(EXIT_FAILURE);}// Create threadpthread_t thdHelloWorld;res = pthread_create(&thdHelloWorld, NULL, helloWorld, NULL);if (res) {printf("Thread creation failed!!/n");exit(EXIT_FAILURE);}while(1) {// Post semaphoresem_post(&binSem);printf("In main, sleep several seconds./n");sleep(1);}// Wait for thread synchronizationvoid *threadResult;res = pthread_join(thdHelloWorld, &threadResult);if (res) {printf("Thread join failed!!/n");exit(EXIT_FAILURE);}exit(EXIT_SUCCESS);
}void* helloWorld(void* arg) {while(1) {// Wait semaphoresem_wait(&binSem);printf("Hello World/n");}
}

编译运行:

[root@localhost semaphore]# gcc semaphore.c-lpthread
[root@localhost semaphore]#./a.out
In main, sleep several seconds.
Hello World
In main, sleep several seconds.
Hello World
In main, sleep several seconds.
Hello World
In main, sleep several seconds.
Hello World

semaphore

   信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确、合理的使用公共资源。
  什么是信号量(Semaphore0
  Semaphore分为单值和多值两种,前者只能被一个线程获得,后者可以被若干个线程获得。
  以一个停车场是运作为例。为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这是如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入一辆,如果又离开两辆,则又可以放入两辆,如此往复。
  在这个停车场系统中,车位是公共资源,每辆车好比一个线程,看门人起的就是信号量的作用。
  更进一步,信号量的特性如下:信号量是一个非负整数(车位数),所有通过它的线程(车辆)都会将该整数减一(通过它当然是为了使用资源),当该整数值为零时,所有试图通过它的线程都将处于等待状态。在信号量上我们定义两种操作: Wait(等待) 和 Release(释放)。 当一个线程调用Wait等待)操作时,它要么通过然后将信号量减一,要么一自等下去,直到信号量大于一或超时。Release(释放)实际上是在信号量上执行加操作,对应于车辆离开停车场,该操作之所以叫做“释放”是应为加操作实际上是释放了由信号量守护的资源。
  实现
  大家都知道,.Net Framework类库中提供的线程同步设施包括:
  Monitor, AutoResetEvent, ManualResetEvent,Mutex,ReadWriteLock和 InterLock。 其中 AutoResetEvent, ManualResetEvent,Mutex派生自WaitHandler,它们实际上是封装了操作系统提供的内核对象。而其它的应当是在.Net虚拟机中土生土长的。显然来自操作系统内核对象的设施使用起来效率要差一些。不过效率并不是我们这里要考虑的问题,我们将使用两个 Monitor 和 一个ManualResetEvent 对象来模拟一个信号量。
  代码如下:
  public class Semaphore
  {
      private ManualResetEvent waitEvent = new ManualResetEvent(false);
      private object syncObjWait = new object();
      private int maxCount = 1; file://最大资源数
      private int currentCount = 0; file://当前资源数
      public Semaphore()
  {
  }
     public Semaphore( int maxCount )
  {
     this.maxCount = maxCount;
  }
  public bool Wait()
  {
     lock( syncObjWait ) file://只能一个线程进入下面代码
     {
         bool waitResult = this.waitEvent.WaitOne(); file://在此等待资源数大于零
         if( waitResult )
         {
           lock( this )
             {
               if( currentCount > 0 )
                 {
                    currentCount--;
                    if( currentCount == 0 )
                     {
                         this.waitEvent.Reset();
                     }
                 }
           else
            {
                 System.Diagnostics.Debug.Assert( false, "Semaphore is not allow current count < 0" );
            }
        }
    }
     return waitResult;
   }
  }
  /** <summary>
  /// 允许超时返回的 Wait 操作
  /// </summary>
  /// <param name="millisecondsTimeout"></param>
  /// <returns></returns>
  public bool Wait( int millisecondsTimeout )
  {
       lock( syncObjWait ) // Monitor 确保该范围类代码在临界区内
          {
             bool waitResult = this.waitEvent.WaitOne(millisecondsTimeout,false);
            if( waitResult )
            {
               lock( this )
               {
                  if( currentCount > 0 )
                  {
                      currentCount--;
                      if( currentCount == 0 )
                       {
                          this.waitEvent.Reset();
                        }
                    }
                 else
                 {
                       System.Diagnostics.Debug.Assert( false, "Semaphore is not allow current count < 0" );
                  }
               }
            }
        return waitResult;
       }
  }
  public bool Release()
  {
          lock( this ) // Monitor 确保该范围类代码在临界区内
         {
             currentCount++;
             if( currentCount > this.maxCount )
            {
               currentCount = this.maxCount;
               return false;
            }
             this.waitEvent.Set(); file://允许调用Wait的线程进入
          }
        return true;
       }
  }

理解Semaphore及其用法详解相关推荐

  1. Future 用法详解

    Future 用法详解 前言 其他知识点 Java 多线程基础 深入理解aqs ReentrantLock用法详解 深入理解信号量Semaphore 深入理解并发三大特性 并发编程之深入理解CAS 深 ...

  2. oracle中的exists 和 not exists 用法详解

    from:http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.html oracle中的exists 和 not exists 用法详解 (2009-05- ...

  3. SVN switch 用法详解 (ZZ)

    SVN switch 用法详解 (ZZ)  http://www.cnblogs.com/dabaopku/archive/2011/05/21/2052820.html 确实,以前不会用switch ...

  4. java super实例_java Super 用法详解及实例代码

    java Super 用法详解及实例代码 发布于 2021-1-8| 复制链接 摘记: java  Super 用法详解 1)有人写了个很好的初始化属性的构造函数,而你仅仅想要在其中添加另一些自己新建 ...

  5. this指针用法详解

    C++中this指针的用法详解 2010-11-12 20:40:45 分类: C/C++ 1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果 ...

  6. python定义类object_Python之ClassObject用法详解

    类和对象的概念很难去用简明的文字描述清楚.从知乎上面的一个回答中可以尝试去理解: 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行 ...

  7. python中的super用法详解_【Python】【类】super用法详解

    一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: class A: def __init__(sel ...

  8. php switch case 判断语句,PHP的switch判断语句的“高级”用法详解,switch详解_PHP教程...

    PHP的switch判断语句的"高级"用法详解,switch详解 只所以称为"高级"用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实 ...

  9. PHP使用Switch语句判断星座,PHP的switch判断语句的“高级”用法详解 用switch语句怎样判断成绩的等级...

    php switch case 求具体详解,case里面能加if语句? swich 语句 我非常喜欢用 case里面加if干嘛 . php switch中能加if语句吗 PHP里 switch cas ...

最新文章

  1. oracle 时间相关
  2. 求解N个值中最大的k个数,N远大于k
  3. mysql 双缓冲_Mysql一些好的优化建议(二)
  4. ASP.Net导出EXCEL表(小结)
  5. idea报“Usage of API documented as @since 1.7”这一问题的解决方法
  6. 【快速入门Linux】7_Linux命令—使用su命令切换用户出现认证失败
  7. Smarty学习笔记(二)
  8. zsh下brew安装
  9. gom及gee小白架设黑屏的原因以及个别装备地图不显示怎么办?
  10. 基于JavaWeb的果蔬生鲜交易系统
  11. 什么都学一点系列之鸿蒙开发Java版简易备忘录
  12. 微信小程序,map地图中在底部添加半透明视图布局
  13. python 制作动画片_分享7个好用的动画制作软件,学会它,人人可以成为动画大师...
  14. 奥比中光进军世界的历程
  15. 软件测试 接口测试 接口测试的必备条件 接口测试用例设计 HTTP协议基础 Postman
  16. 量化分析师的Python日记【Q Quant兵器谱 -之偏微分方程1】
  17. 微信企业号开发—通讯录
  18. 零中频接收机频率转换图_初探“光通信”光接收机
  19. HTTP返回结果状态码小结
  20. java poi 通用工具类

热门文章

  1. ztree同级只显示一个节点
  2. 回答朋友的问题(关于 RTEMS 学习)
  3. 大数据分析要避免哪些错误
  4. 统计某一范围内所有的是K的倍数或者含有K的整数
  5. TypeScript算法专题 - [双链表1] - 双链的概念及其实现
  6. js两个问号代表什么_js中的Object.assign接受两个函数为参数的时候会发生什么?...
  7. LRU算法的实现(STL+模拟)
  8. java generatedvalue_java-@GeneratedValue和@GenericGen之间的区别
  9. 推荐 | 微软SAR近邻协同过滤算法拆解(二)
  10. Tomcat(Install)