线程同步之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 ...
随机推荐
- python机器学习sklearn 岭回归(Ridge、RidgeCV)
1.介绍 Ridge 回归通过对系数的大小施加惩罚来解决 普通最小二乘法 的一些问题. 岭系数最小化的是带罚项的残差平方和, 其中,α≥0α≥0 是控制系数收缩量的复杂性参数: αα 的值越大,收缩量 ...
- [Python]多线程入门
原文:http://blog.csdn.net/ice110956/article/details/28421807 Python的多线程有两种实现方法: 函数,线程类 1.函数 调用thread模块 ...
- oauth2-server-php-docs 授权类型
授权码 概观 在Authorization Code交付式时使用的客户端想要请求访问受保护资源代表其他用户(即第三方).这是最常与OAuth关联的授予类型. 详细了解授权码 用例 代表第三方来电 履行 ...
- ASP入门(十三)-Server对象
Server 对象用于处理服务器上的一些特殊任务,例如,创建组件实例.获取文件路径.执行ASP脚本文件等. Server 对象是体现 ASP 强大功能的一个对象,之前介绍的对象都是针对获取.请求以及简 ...
- chrome插件编写基本入门
chrome插件编写基本入门 http://igeekbar.com/igeekbar/post/331.htm #精选JAVASCRIPTCHROME 作为一名程序猿,怎么能不会写chrome插件 ...
- Dapper - .Net 环境下一个简单对象映射的框架
本文内容 特点 性能 参数化的查询 List 支持 缓存和非缓存的 readers 多个映射 多个结果 存储过程 Ansi Strings 和 varchar 限制和注意事项 Dapper 能运行在我 ...
- NVIDIA安装显卡提示你必须先安装Intel怎么办
无法安装驱动程序 计算机管理中查看当前的Intel的显卡驱动是否已经安装成功,如果显示的是标准VGA,则没有安装驱动,先要把Intel的显卡驱动装好(有时候360驱动大师这种工具并不能自动帮你装好 ...
- ZH奶酪:PHP error_log()将错误信息写入日志文件
error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段. bool error_log ( string $message [, int $messag ...
- 【树莓派】服务配置相关3:基于Ubuntu Server的服务配置
该文接续之前写过的两篇: [树莓派]服务配置相关 [树莓派]服务配置相关2:基于RPi Desktop的服务配置 这是我个人用来进行树莓派盒子安装配置的脚本,对于外部其他博友,可以部分参考,但不需要逐 ...
- 教你轻松计算AOE网关键路径
认识AOE网 有向图中,用顶点表示活动,用有向边表示活动之间开始的先后顺序,则称这种有向图为AOV网络:AOV网络可以反应任务完成的先后顺序(拓扑排序). 在AOV网的边上加上权值表示完成该活动所需的 ...