https://msdn.microsoft.com/en-us/library/z6zx288a(v=vs.110).aspx

The System.Threading.Semaphore class represents a named (systemwide) or local semaphore.

It is a thin wrapper around the Win32 semaphore object.

Win32 semaphores are counting semaphores, which can be used to control access to a pool of resources.

The SemaphoreSlim class represents a lightweight, fast semaphore that can be used for waiting within a single process when wait times are expected to be very short.

SemaphoreSlim relies as much as possible on synchronization primitives provided by the common language runtime (CLR).

However, it also provides lazily initialized, kernel-based wait handles as necessary to support waiting on multiple semaphores.

SemaphoreSlim also supports the use of cancellation tokens, but it does not support named semaphores or the use of a wait handle for synchronization.

Managing a Limited Resource

Threads enter the semaphore by calling the WaitOne method, which is inherited from the WaitHandle class,in the case of a System.Threading.Semaphore object,

or the SemaphoreSlim.Wait or SemaphoreSlim.WaitAsync method, in the case of a SemaphoreSlim object..

When the call returns, the count on the semaphore is decremented.

When a thread requests entry and the count is zero, the thread blocks.

As threads release the semaphore by calling the Semaphore.Release or SemaphoreSlim.Release method, blocked threads are allowed to enter.

There is no guaranteed order, such as first-in, first-out (FIFO) or last-in, first-out (LIFO), for blocked threads to enter the semaphore.

A thread can enter the semaphore multiple times by calling the System.Threading.Semaphore object's WaitOne method or the SemaphoreSlimobject's Wait method repeatedly.

To release the semaphore, the thread can either call the Semaphore.Release() or SemaphoreSlim.Release() method overload the same number of times,

or call the Semaphore.Release(Int32) or SemaphoreSlim.Release(Int32) method overload and specify the number of entries to be released.

Semaphores and Thread Identity

The two semaphore types do not enforce thread identity on calls to the WaitOneWaitRelease, and SemaphoreSlim.Release methods.

For example, a common usage scenario for semaphores involves a producer thread and a consumer thread, with one thread always incrementing the semaphore count and the other always decrementing it.

It is the programmer's responsibility to ensure that a thread does not release the semaphore too many times.

For example, suppose a semaphore has a maximum count of two, and that thread A and thread B both enter the semaphore.

If a programming error in thread B causes it to call Release twice, both calls succeed.

The count on the semaphore is full, and when thread A eventually calls Release, , a SemaphoreFullException is thrown.

Named Semaphores

The Windows operating system allows semaphores to have names.

A named semaphore is system wide.

That is, once the named semaphore is created, it is visible to all threads in all processes.

Thus, named semaphore can be used to synchronize the activities of processes as well as threads.

You can create a Semaphore object that represents a named system semaphore by using one of the constructors that specifies a name.

Note:

Because named semaphores are system wide, it is possible to have multiple Semaphore objects that represent the same named semaphore.

Each time you call a constructor or the Semaphore.OpenExisting method, a new Semaphore object is created.

Specifying the same name repeatedly creates multiple objects that represent the same named semaphore.

Be careful when you use named semaphores.

Because they are system wide, another process that uses the same name can enter your semaphore unexpectedly.

Malicious恶意的 code executing on the same computer could use this as the basis基础 of a denial-of-service拒绝服务 attack.

Use access control security to protect a Semaphore object that represents a named semaphore, preferably by using a constructor that specifies a System.Security.AccessControl.SemaphoreSecurity object.

You can also apply access control security using the Semaphore.SetAccessControl method, but this leaves a window of vulnerability弱点 between the time the semaphore is created and the time it is protected.

Protecting semaphores with access control security helps prevent malicious attacks, but does not solve the problem of unintentional非故意的 name collisions冲突.

Semaphore and SemaphoreSlim的更多相关文章

  1. 一次 .NET Core 中玩锁的经历:ManualResetEventSlim, Semaphore 与 SemaphoreSlim

    最近同事对  .net core memcached 缓存客户端 EnyimMemcachedCore 进行了高并发下的压力测试,发现在 linux 上高并发下使用 async 异步方法读取缓存数据会 ...

  2. 信号量-Semaphore、SemaphoreSlim

    核心类:Semaphore,通过int数值来控制线程个数. * 通过观察构造函数 public Semaphore(int initialCount, int maximumCount);: * in ...

  3. C#信号量(Semaphore,SemaphoreSlim)

    Object->MarshalByRefObject->WaitHandle->Semaphore 1.作用: 多线程环境下,可以控制线程的并发数量来限制对资源的访问 2.举例: S ...

  4. Mutex vs Semaphore vs Monitor vs SemaphoreSlim

    C#开发者(面试者)都会遇到Mutex,Semaphore,Monitor,SemaphoreSlim这四个与锁相关的C#类型,本文期望以最简洁明了的方式阐述四种对象的区别. 线程安全 教条式理解 如 ...

  5. 企业应用架构研究系列二十六:信号量SemaphoreSlim与Semaphore

    在进行多线程程序的开发和设计的过程中,不可避免的需要引入semaphore信号量这个组件,这是.net框架提供的一个对多线程计数互斥的方案,就是允许指定的线程个数访问特定的资源而增加的 一个" ...

  6. 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent

    [源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...

  7. [.net 多线程]Semaphore信号量

    信号量(Semaphore)是一种CLR中的内核同步对象.与标准的排他锁对象(Monitor,Mutex,SpinLock)不同的是,它不是一个排他的锁对象,它与SemaphoreSlim,Reade ...

  8. C# 多线程访问之 SemaphoreSlim(信号量)【C# 进阶】

    SemaphoreSlim 是对可同时访问某一共享资源或资源池的线程数加以限制的 Semaphore 的轻量替代,也可在等待时间预计很短的情况下用于在单个进程内等待. 由于 SemaphoreSlim ...

  9. .Net SemaphoreSlim

    看Elsa-core源代码中看到的,Elsa-core中所有保存数据的方法似乎使用同一个Save方法.如下图: 那么为什么要使用这玩意,我还是头一次见这玩意???? 好吧,我承认我自己菜.我自个儿也该 ...

随机推荐

  1. 【讲●解】火车进出栈类问题 & 卡特兰数应用

    火车进出栈类问题详讲 & 卡特兰数应用 引题:火车进出栈问题 [题目大意] 给定 \(1\)~\(N\) 这\(N\)个整数和一个大小无限的栈,每个数都要进栈并出栈一次.如果进栈的顺序为 \( ...

  2. HDOJ 1846 Brave Game - 博弈入门

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1846 经典基础博弈,首先面对(m+1)的人一定会输,依次往后推即可: #include<iost ...

  3. librdkafka使用VS2015进行编译

    抄了那么久的<kafka权威指南>,开始实操了,按照书本的介绍,kafka本身提供针对Java的原生API,其它语言如果需要使用kafka,那么就需要通过第三方库来做了,对了再书中一直提及 ...

  4. Linux部署Web项目小记

    1.安装Tomcat 官网下载 解压缩:tar -zxvf apache-tomcat-8.0.32.tar.gz 配置server.xml 连接池: <Executor name=" ...

  5. Groovy常用语法汇总

    基本语法 1.Grovvy的注释分为//和/**/和java的一样. 2.Grovvy语法可以不已分号结尾. 3.单引号,里面的内容严格的对应java中的String,不对$符号进行转义. def s ...

  6. [Python3网络爬虫开发实战] 1.9.2-Scrapyd的安装

    Scrapyd是一个用于部署和运行Scrapy项目的工具,有了它,你可以将写好的Scrapy项目上传到云主机并通过API来控制它的运行. 既然是Scrapy项目部署,基本上都使用Linux主机,所以本 ...

  7. 树莓派 -- oled 续(1) wiringPi

    在上文中,分析了wiringPi 的oled demo是使用devfs来控制spi master和spi slave通讯. https://blog.csdn.net/feiwatson/articl ...

  8. 使用Hashids来保护你的数据库主键

    为什么要保护数据库主键? 数据库主键一般是有序自增主键,极易被爬虫抓取数据,作为应用开发者,这是不应该的,你辛辛苦苦收集的数据转眼之间被其他人给抓取了,是不是很大的损失? Hashids的介绍 gen ...

  9. Matlab学习笔记(四)

    二.MATLAB基础知识 (六)字符串 字符串的创建和简单操作 用单引号对括起来的一系列字符的组合,每个字符是一个元素,通常通过两个字节来存储 表2-22    字符串常见操作函数(e_two_37. ...

  10. noi.ac NOIP2018 全国热身赛 第二场 T3 color

    [题解] 我们可以发现每次修改之后叶子结点到根的路径最多分为两段:一段白色或者黑色,上面接另一段灰色的.二分+倍增找到分界点,然后更新答案即可. check的时候只需要判断当前节点对应的叶子结点的区间 ...