namespace RealitySimulation.Utils.Threading
{
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
#endregion
///
1<summary>
2/// 同步屏障.
3/// </summary>
public interface ISyncBarrier
{
///
1<summary>
2/// 获取正在有屏障处理的线程总数.
3/// </summary>
///
1<remarks>
2/// 改变线程总数,是一个同步操作.
3/// 请在确保没有任何线程使用当前
4/// 屏障(或初始化)时改动总数.如果已经有线
5/// 程使用屏障,请使用ExchangingThreads
6/// 异步更改线程的数量.
7/// </remarks>
int TotalThreads { get;set;}
///
1<summary>
2/// 获取或者设置等待交换的线程数.
3/// </summary>
///
1<remarks>
2/// 可以通过交换向线程总数中
3/// 添加或者删除线程.
4/// 使用这种方法主要是因为
5/// 线程同步过程中,只能在
6/// 重建屏障的时候改变线程
7/// 总数.而其他时候将造成
8/// 同步的失败.
9/// 此值为正,意味着添加
10/// 线程,相反意味着删除
11/// 线程.
12/// 当交换完成,此值自动
13/// 置零.
14/// 此外,如果线程的减少
15/// 量大于当前线程总量,
16/// 直接将总量置零,而不
17/// 考虑余量.
18/// 注意,这是一个异步操作,
19/// 屏障可能不会立即实现
20/// 这个操作.请观察此值,
21/// 以确定是否进行添加.
22/// </remarks>
int ExchangingThreads { get;set;}
///
1<summary>
2/// 获取当前被阻碍的线程数.
3/// </summary>
int BarricadedThreads { get;}
///
1<summary>
2/// 穿越屏障.
3/// </summary>
///
1<remarks>
2/// 线程通过调用穿越屏障,
3/// 将对控制的安排交给屏障.
4/// 当逃离条件满足,也就是
5/// 到达线程数量已经达到
6/// 要求的数量,屏障将自动
7/// 开放,而线程在此时可以
8/// 穿越.当逃离条件尚未满
9/// 足,线程将在穿越屏障的
10/// 时候被屏障阻塞.
11/// </remarks>
void Stride();
}
public class SyncBarrier:ISyncBarrier
{
protected int unbarricadedThreads = 0;
protected int exchangingThreads = 0;
protected int totalThreads = 0;
public virtual int BarricadedThreads
{
get { return this.totalThreads - this.unbarricadedThreads; }
}
public virtual int TotalThreads
{
get { return this.totalThreads; }
set { this.Rebuild(value); }
}
public virtual int ExchangingThreads
{
get { return this.exchangingThreads; }
set { this.exchangingThreads = value; }
}
public SyncBarrier():this(1){}
public SyncBarrier(int totalThreads)
{
this.Rebuild(totalThreads);
}
public virtual void Stride()
{
//Barrier is designed for threads, no thread takes no effect.
//And single thread will not be barricaded.
if (this.totalThreads > 0)
{
lock (this)
{
if (this.unbarricadedThreads > 0)
{
this.unbarricadedThreads--;
//All threads arrived
if (this.unbarricadedThreads == 0)
{
//Rebuild first, then pulse the signal!
this.Rebuild(this.totalThreads+this.exchangingThreads);
Monitor.PulseAll(this);
}
else//Not all threads arrived.
{
Monitor.Wait(this);
}
//Threads escapes here.
}
}
}
}
protected virtual void Rebuild(int totalThreads)
{
this.unbarricadedThreads = this.totalThreads = totalThreads;
this.exchangingThreads = 0;
}
}
}