表示之前对semaphore信号量木有神码概念。

比较纳闷这玩意要干嘛,好吧继续stackflow:

Mutex can be released only by thread that had acquired it, while you can signal semaphore from any other thread (or process), so semaphores are more suitable for some synchronization problems like producer-consumer.

意思就是会所semaphore可以被任何线程或者进程释放,在生产者和消费者模型里用的比较多;

而mutex只能被拥有它的线程被释放,但如果这个线程突然当掉了,操作系统会负责释放它。

On Windows, binary semaphores are more like event objects than mutexes.

另一个拿分的解释是:

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

mutex是厕所钥匙,一次只能一人那着这把钥匙去厕所。结束了,这个人把钥匙给队列中的下一个人。

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.

信号量是一个自由的官方厕所钥匙,我们有四个厕所,他们的锁和钥匙是一样的。信号量开始设置为4,表示4个厕所是自由滴,如果一个人进去了,数量就-1.如果厕所满了,钥匙数目就为0,信号量数目这时也是0.如果一个人离开厕所,信号量+!,队列中的下一个人可以用啦!

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和Semaphore的更多相关文章

  1. 线程同步之mutex和event区别

    之前只是用过 关键段来对同进程不同线程进行互斥,防止对同一份资源或代码段的竞争: mutex可以理解为不同进程或者同一进程内防止对同一份资源的竞争: event更多的是同步,当然也是不同进程或者同一进 ...

  2. C#编程总结(三)线程同步

    C#编程总结(三)线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多 ...

  3. [转]C#编程总结(三)线程同步

    本文转自:http://www.cnblogs.com/yank/p/3227324.html 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可 ...

  4. .net线程同步

    大家都晓得.NET中线程同步有以下几种方式: 临界区(Critical Section).互斥量(Mutex).信号量(Semaphore).事件(Event) 1.临界区:通过对多线程的串行化来访问 ...

  5. windows线程同步的总结

    一 线程 1)如果你正在编写C/C++代码,决不应该调用CreateThread.相反,应该使用VisualC++运行期库函数_beginthreadex,退出也应该使用_endthreadex.如果 ...

  6. C#线程同步(转)

    线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多线程处理提供了用不 ...

  7. C#线程同步(4)- 通知&EventWaitHandle一家

    文章原始出处 http://xxinside.blogbus.com/logs/47523285.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...

  8. .NET中线程同步的几种方法

    lock.Monitor: lock是Monitor的语法糖 [MethodImpl(MethodImplOptions.Synchronized)]: 特性标记一个方法是需要被同步的,最终实现的效果 ...

  9. Posix线程编程指南(3) 线程同步

    互斥锁 尽管在Posix Thread中同样可以使用IPC的信号量机制来实现互斥锁mutex功能,但显然semphore的功能过于强大了,在Posix Thread中定义了另外一套专门用于线程同步的m ...

随机推荐

  1. 学 Win32 汇编[33] - 探讨 Win32 汇编的模块化编程

    我觉得所谓的模块化有两种: "假模块化" 和 "真模块化". 所谓 "假模块化" 就是通过 include 指令把 *.inc 或 *.as ...

  2. Asp.net MVC利用WebUploader上传大文件出现404解决办法。

    刚开始我上传小文件都是比较顺利的,但是上传了一个大文件大约有200M的压缩包就不行了.在chrome里面监视发现网络状态是404,我分析可能不是WebUploader的限制,应该是WebConfig限 ...

  3. MongoDB数据库遭大规模勒索攻击,被劫持26000多台服务器 #精选GITHUBMYSQL

    昨天,一个大新闻爆出,MongoDB数据库叕被攻击了.就在上周末,三个黑客团伙劫持了MongoDB逾26000多台服务器,其中规模最大的一组超过22000台. “MongoDB启示录”再临?   此次 ...

  4. AI单挑Dota 2世界冠军:被电脑虐哭……

    OpenAI的机器人刚刚在 Dota2 1v1 比赛中战胜了人类顶级职业玩家 Denti.以建设安全的通用人工智能为己任的 OpenAI,通过“Self-Play”的方式,从零开始训练出了这个机器人. ...

  5. CentOS6.5安装nginx1.5.8时出现“cp: "conf/koi-win" 与"/usr/local/nginx/conf/koi-win" 为同一文件”的解决方法

    安装方法主要参考了:http://www.cnblogs.com/zhoulf/archive/2013/02/09/2909653.html这篇文章,出现“cp: "conf/koi-wi ...

  6. destoon源码解读

    一.module module值:表示模块的id ID1.核心: ID2.会员: ID3.扩展: 当ID>3时,为购买.公司等模块. dt:为各种变量,相当于整站的配置,如:关键词.描述.积分等 ...

  7. Core Java-多线程-线程的生命周期

    0. 在介绍线程前我们先看一下什么是进程? 进程是线程的母亲,如果在大学计算机课程里读过操作系统一定不会陌生. 所谓进程,它是计算机程序关于某数据集上的一次活动,是系统进行资源分配和调度的基本单位,是 ...

  8. 牛客网-《剑指offer》-变态跳台阶

    C++ class Solution { public: int jumpFloorII(int n) { <<--n; } }; 推导: 关于本题,前提是n个台阶会有一次n阶的跳法.分析 ...

  9. Android 如何关闭Navigation Bar M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  10. Echart 仪表盘和柱形图

    我们来分布讲解: 1.首先编一写一个html,如下: <html> <body class=""> <div class="containe ...