ManualResetEventSlim通过封装 ManualResetEvent提供了自旋等待和内核等待的组合。如果需要跨进程或者跨AppDomain的同步,那么就必须使用ManualResetEvent,而不能使用ManualResetEventSlim。那么首先我们看看 ManualResetEvent和AutoResetEvent的使用特点,只有搞清楚了ManualResetEvent才可能明白ManualResetEventSlim的好处。

ManualResetEvent和AutoResetEvent的共同点:
1)Set方法将事件状态设置为终止状态,允许一个或多个等待线程继续;Reset方法将事件状态设置为非终止状态,导致线程阻止;WaitOne阻止当前线程,直到当前线程的WaitHandler收到事件信号。
2)可以通过构造函数的参数值来决定其初始状态,若为true则事件为终止状态从而使线程为非阻塞状态,为false则线程为阻塞状态。
3)如果某个线程调用WaitOne方法,则当事件状态为终止状态时,该线程会得到信号,继续向下执行。

ManualResetEvent和AutoResetEvent的不同点:
1)AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只有继续等待,也就是说AutoResetEvent一次只唤醒一个线程;
2)ManualResetEvent则可以唤醒多个线程,因为当某个线程调用了ManualResetEvent.Set()方法后,其他调用WaitOne的线程获得信号得以继续执行,而ManualResetEvent不会自动将信号置为不发送。
3)也就是说,除非手工调用了ManualResetEvent.Reset()方法,则ManualResetEvent将一直保持有信号状态,ManualResetEvent也就可以同时唤醒多个线程继续执行。

AutoResetEvent myResetEvent = new AutoResetEvent(false)
构造方法的参数设置成false后,表示创建一个没有被set的AutoResetEvent,这就导致所有持有这个AutoResetEvent的线程都会在WaitOne()处挂起, 如果将参数设置成true,表示创建一个被set的AutoResetEvent,持有这个AutoResetEvent的线程们会竞争这个Event ,此时在其他条件满足的情况下,至少会有一个线程得到执行,而不是因得不到Event而导致所有线程都得不到执行

ManualResetEvent myResetEvent = new ManualResetEvent(false)
构造方法的参数设置成false后,表示创建一个没有被set的ManualResetEvent,这就导致所有持有这个ManualResetEvent的线程都会在WaitOne()处挂起 ,如果将参数设置成true,表示创建一个被set的ManualResetEvent ,持有这个ManualResetEvent的线程们在其他条件满足的情况下会同时得到执行(注意,是同时得到执行);而不是因得不到Event而导致所有线程都得不到执行

我们来看看ManualResetEventSlim的实现:

public class ManualResetEventSlim : IDisposable
{
private volatile object m_lock;
// A lock used for waiting and pulsing. Lazily initialized via EnsureLockObjectCreated()
private volatile ManualResetEvent m_eventObj; // A true Win32 event used for waiting.
private const int DEFAULT_SPIN_MP = SpinWait.YIELD_THRESHOLD; //
public ManualResetEventSlim(bool initialState)
{
// Specify the defualt spin count, and use default spin if we're
// on a multi-processor machine. Otherwise, we won't.
Initialize(initialState, DEFAULT_SPIN_MP);
}
public void Set()
{
Set(false);
}
private void Set(bool duringCancellation)
{
// We need to ensure that IsSet=true does not get reordered past the read of m_eventObj
// This would be a legal movement according to the .NET memory model.
// The code is safe as IsSet involves an Interlocked.CompareExchange which provides a full memory barrier.
IsSet = true; // If there are waiting threads, we need to pulse them.
if (Waiters > 0)
{
Contract.Assert(m_lock != null); //if waiters>0, then m_lock has already been created.
lock (m_lock)
{
Monitor.PulseAll(m_lock);
}
}
ManualResetEvent eventObj = m_eventObj;
if (eventObj != null && !duringCancellation)
{
lock (eventObj)
{
if (m_eventObj != null)
{
// If somebody is waiting, we must set the event.
m_eventObj.Set();
}
}
}
}
public void Reset()
{
ThrowIfDisposed();
// If there's an event, reset it.
if (m_eventObj != null)
{
m_eventObj.Reset();
}
IsSet = false;
}
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
{
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested(); // an early convenience check if (millisecondsTimeout < -)
{
throw new ArgumentOutOfRangeException("millisecondsTimeout");
}
if (!IsSet)
{
if (millisecondsTimeout == )
{
// For 0-timeouts, we just return immediately.
return false;
}
// We spin briefly before falling back to allocating and/or waiting on a true event.
uint startTime = ;
bool bNeedTimeoutAdjustment = false;
int realMillisecondsTimeout = millisecondsTimeout; //this will be adjusted if necessary. if (millisecondsTimeout != Timeout.Infinite)
{
startTime = TimeoutHelper.GetTime();
bNeedTimeoutAdjustment = true;
}
//spin
int HOW_MANY_SPIN_BEFORE_YIELD = ;
int HOW_MANY_YIELD_EVERY_SLEEP_0 = ;
int HOW_MANY_YIELD_EVERY_SLEEP_1 = ; int spinCount = SpinCount;
for (int i = ; i < spinCount; i++)
{
if (IsSet)
{
return true;
}
else if (i < HOW_MANY_SPIN_BEFORE_YIELD)
{
if (i == HOW_MANY_SPIN_BEFORE_YIELD / 2)
{
Thread.Yield();
}
else
{
Thread.SpinWait(PlatformHelper.ProcessorCount * (4 << i));
}
}
else if (i % HOW_MANY_YIELD_EVERY_SLEEP_1 == 0)
{
Thread.Sleep(1);
}
else if (i % HOW_MANY_YIELD_EVERY_SLEEP_0 == 0)
{
Thread.Sleep(0);
}
else
{
Thread.Yield();
}
if (i >= 100 && i % 10 == 0) // check the cancellation token if the user passed a very large spin count
cancellationToken.ThrowIfCancellationRequested();
} // Now enter the lock and wait.
EnsureLockObjectCreated(); // We must register and deregister the token outside of the lock, to avoid deadlocks.
using (cancellationToken.InternalRegisterWithoutEC(s_cancellationTokenCallback, this))
{
lock (m_lock)
{
// Loop to cope with spurious wakeups from other waits being canceled
while (!IsSet)
{
// If our token was canceled, we must throw and exit.
cancellationToken.ThrowIfCancellationRequested(); //update timeout (delays in wait commencement are due to spinning and/or spurious wakeups from other waits being canceled)
if (bNeedTimeoutAdjustment)
{
realMillisecondsTimeout = TimeoutHelper.UpdateTimeOut(startTime, millisecondsTimeout);
if (realMillisecondsTimeout <= )
return false;
}
Waiters = Waiters + 1;
if (IsSet) //This check must occur after updating Waiters.
{
Waiters--; //revert the increment.
return true;
} // Now finally perform the wait.
try
{
// ** the actual wait **
if (!Monitor.Wait(m_lock, realMillisecondsTimeout))
return false; //return immediately if the timeout has expired.
}
finally
{
// Clean up: we're done waiting.
Waiters = Waiters - 1;
}
}
}
}
} // automatically disposes (and deregisters) the callback return true; //done. The wait was satisfied.
}
private void EnsureLockObjectCreated()
{
Contract.Ensures(m_lock != null);
if (m_lock != null)
return;
object newObj = new object();
Interlocked.CompareExchange(ref m_lock, newObj, null); // failure is benign.. someone else won the ----.
}
private static Action<object> s_cancellationTokenCallback = new Action<object>(CancellationTokenCallback);
private static void CancellationTokenCallback(object obj)
{
ManualResetEventSlim mre = obj as ManualResetEventSlim;
Contract.Assert(mre != null, "Expected a ManualResetEventSlim");
Contract.Assert(mre.m_lock != null); //the lock should have been created before this callback is registered for use.
lock (mre.m_lock)
{
Monitor.PulseAll(mre.m_lock); // awaken all waiters
}
}
}
public sealed class ManualResetEvent : EventWaitHandle
{
public ManualResetEvent(bool initialState) : base(initialState,EventResetMode.ManualReset){}
}

其中的Reset方法最简单就是调用 ManualResetEvent的Reset方法,Set方法也是调用ManualResetEvent的Set方法,只是在Set方法前需要把等待队列的线程转换为就绪状态【lock (m_lock){Monitor.PulseAll(m_lock);}】,ManualResetEventSlim 与ManualResetEvent的区别主要是Wait方法里面增加了自旋。

这里面的using (cancellationToken.InternalRegisterWithoutEC(s_cancellationTokenCallback, this))也是非常重要,Monitor.Wait方法只是把线程放到等待队列,调用ManualResetEvent的Set方法会调用   Monitor.PulseAll(m_lock);,但是在调用ManualResetEvent的wait方法,里面调用了cancellationToken.ThrowIfCancellationRequested()该如何处理,这个时候的lock锁没有释放,需要调用 Monitor.PulseAll方法,所以该方法被方放到CancellationTokenCallback里面

C# ManualResetEventSlim 实现的更多相关文章

  1. C#并行编程 (Barrier,CountdownEvent,ManualResetEventSlim,SemaphoreSlim,SpinLock,SpinWait )

    背景 有时候必须访问变量.实例.方法.属性或者结构体,而这些并没有准备好用于并发访问,或者有时候需要执行部分代码,而这些代码必须单独运行,这是不得不通过将任务分解的方式让它们独立运行. 当任务和线程要 ...

  2. .net core WebApi Interlocked配合ManualResetEventSlim实现并发同步

    由于项目有某种需求,在WebApi中,有大量的请求需要操作相同的数据,因此需要用到并发同步机制去操作共享的数据. 本次配合使用Interlocked和ManualResetEventSlim来实现并发 ...

  3. 《C#多线程编程实战》2.6 ManualResetEventSlim

    这个比较好理解的. 正如书上所言,如同一直在打开的大门的屋子,谁要进去,谁就自己的关门,出来的时候在开开. 常用的方法 有三个: Set()  //设置为有信号,也就是让等待的线程不用继续等待,唤醒等 ...

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

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

  5. 线程之间灵活传递信号(ManualResetEventSlim )

    当主程序启动时,首先创建ManualResetEventSlim 类的一个实例.然后启动三个线程,等待事件信号通知它们继续执行. /// <summary> /// 创建 ManualRe ...

  6. C#多线程之线程同步篇3

    在上一篇C#多线程之线程同步篇2中,我们主要学习了AutoResetEvent构造.ManualResetEventSlim构造和CountdownEvent构造,在这一篇中,我们将学习Barrier ...

  7. C#多线程之线程同步篇2

    在上一篇C#多线程之线程同步篇1中,我们主要学习了执行基本的原子操作.使用Mutex构造以及SemaphoreSlim构造,在这一篇中我们主要学习如何使用AutoResetEvent构造.Manual ...

  8. C#多线程之线程同步篇1

    在多线程(线程同步)中,我们将学习多线程中操作共享资源的技术,学习到的知识点如下所示: 执行基本的原子操作 使用Mutex构造 使用SemaphoreSlim构造 使用AutoResetEvent构造 ...

  9. C# 线程同步的三类情景

    C# 已经提供了我们几种非常好用的类库如 BackgroundWorker.Thread.Task等,借助它们,我们就能够分分钟编写出一个多线程的应用程序. 比如这样一个需求:有一个 Winform ...

随机推荐

  1. ansible 自动化管理

    1 什么样的情形需要批量部署 1.操作系统的安装 常见的有collber,red hat satelite(redhat)系统专用. 2.操作系统的配置 常见的有cfengine,puppet,che ...

  2. JavaSE| 数组

    1.数组(array) 数组就是多个相同类型数据的组合,实现对这些数据的统一管理. 数组中的元素可以是任何数据类型,包括基本数据类型和引用数据类型.数组属引用类型,数组型数据是对象(object),每 ...

  3. Linux 文件目录结构

    /           文件系统的入口,最高一级目录 /bin           存放系统所需的基本命令 /boot     包含Linux内核及系统引导程序所需要的文件 ( 如连接文件,镜像文件等 ...

  4. 完全背包-hdu1114

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目描述: 代码实现: #include<cstdio> #include<i ...

  5. vue源码的构建

    一.vue构建的基本了解 1,开始学习vue的源码的学习,vue.js是基于rollup构建的 它的配置在 scripts下面 rollup是webpack的简小版针对于js进行压缩的,没有提供复杂的 ...

  6. [洛谷P1063][NOIP2006]能量项链

    区间DP模板题 题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗 ...

  7. [洛谷P1880][NOI1995]石子合并

    区间DP模板题 区间DP模板Code: ;len<=n;len++) { ;i<=*n-;i++) //区间左端点 { ; //区间右端点 for(int k=i;k<j;k++) ...

  8. AS安装过程中出现的错误

    1.首先是You may need to adjust the proxy settings in Gradle.的错误, 主要是看你有没有图中红线所画的gradle的压缩包 如果没有,那就前往htt ...

  9. 深度学习目标检测:RCNN,Fast,Faster,YOLO,SSD比较

    转载出处:http://blog.csdn.net/ikerpeng/article/details/54316814 知乎的图可以放大,更清晰,链接:https://www.zhihu.com/qu ...

  10. HOJ3237----BFS/DFS

    /* 注意两点 . 不可以使用替换可用节点为不可用节点的方法进行DFS 因为角落也可能有油,替换了就出不来.(某学长指导) . 可用通过开一个数组(例如我的b[][]数组) 用了存储到当前位置剩余最大 ...