线程同步之mutex和Semaphore
表示之前对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的更多相关文章
- 线程同步之mutex和event区别
之前只是用过 关键段来对同进程不同线程进行互斥,防止对同一份资源或代码段的竞争: mutex可以理解为不同进程或者同一进程内防止对同一份资源的竞争: event更多的是同步,当然也是不同进程或者同一进 ...
- C#编程总结(三)线程同步
C#编程总结(三)线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多 ...
- [转]C#编程总结(三)线程同步
本文转自:http://www.cnblogs.com/yank/p/3227324.html 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可 ...
- .net线程同步
大家都晓得.NET中线程同步有以下几种方式: 临界区(Critical Section).互斥量(Mutex).信号量(Semaphore).事件(Event) 1.临界区:通过对多线程的串行化来访问 ...
- windows线程同步的总结
一 线程 1)如果你正在编写C/C++代码,决不应该调用CreateThread.相反,应该使用VisualC++运行期库函数_beginthreadex,退出也应该使用_endthreadex.如果 ...
- C#线程同步(转)
线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多线程处理提供了用不 ...
- C#线程同步(4)- 通知&EventWaitHandle一家
文章原始出处 http://xxinside.blogbus.com/logs/47523285.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...
- .NET中线程同步的几种方法
lock.Monitor: lock是Monitor的语法糖 [MethodImpl(MethodImplOptions.Synchronized)]: 特性标记一个方法是需要被同步的,最终实现的效果 ...
- Posix线程编程指南(3) 线程同步
互斥锁 尽管在Posix Thread中同样可以使用IPC的信号量机制来实现互斥锁mutex功能,但显然semphore的功能过于强大了,在Posix Thread中定义了另外一套专门用于线程同步的m ...
随机推荐
- Spark的动态资源分配
跑spark程序的时候,公司服务器需要排队等资源,参考一些设置,之前不知道,跑的很慢,懂得设置之后简直直接起飞. 简单粗暴上设置代码: def conf(self): conf = super(Tbt ...
- XenServer修改DNS
XenServer没法直接修改DNS,感觉好奇怪啊 修改方法: 1.进入命令行: 2.执行命令: # xe pif-list 列出网卡的UUID. 3.执行命令: # xe p ...
- [Canvas]碰撞球 增加小球间碰撞检测
请点此下载代码并用Chrome或是Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8"&g ...
- SSH连接linux时,长时间不操作就断开的解决方案
转自:http://www.cnblogs.com/jifeng/archive/2011/06/25/2090118.html 修改/etc/ssh/sshd_config文件,找到 ClientA ...
- SVN 配置文件说明
svnserve是SVN自带的一个轻型服务器,客户端通过使用以svn://或svn+ssh://为前缀的URL来访问svnserve服务器,实现远程访问SVN版本库.svnserve可以通过配置文件来 ...
- 自定义控件之万能Repeater源码
using System.ComponentModel; using System.Web.UI; [assembly: TagPrefix("Jinlong.Control", ...
- LintCode: Flatten Binary Tree to Linked List
C++ Traverse /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, ...
- python globals和locals
文章里面说globals和locals函数返回的是命名空间 - 一个存有对应作用域的所有的变量.方法的字典,注意这里和dir函数返回数组的不一样 Python命名空间的本质 class Test(ob ...
- QT之设计部件背景色
一.使用QT样式表设计部件外观 样式表使用文本描写叙述,能够使用QApplication::setStyleSheet()函数将其设置到整个应用程序上.也能够使用QWidget::setStyleSh ...
- python操作Excel的几种方式
Python对Excel的读写主要有xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种. 1.xlrd主要是用来读取excel文件 import xlrd workbook ...