首页 > 代码库 > [.net]基元线程同步构造

[.net]基元线程同步构造

  1 /* 基元线程同步构造  2 用户模式构造:  3     易变构造(Volatile Construct)  4     互锁构造(Interlocked Construct):自旋锁(Spinlock) 乐观锁(Optimistic Concurrency Control,乐观并发控制)  5 内核模式构造:  6     事件构造(Event)  7     信号量构造(Semaphore)  8     互斥体构造(Mutex)  9 */ 10  11 //易变构造,Volatile.Write()之前的所有字段写入操作,必须再该方法调用之前完成,Volatile.Read()之前的所有字段读取操作,必须再该方法之前完成,保证该方法作用的字段 12 //的赋值或读取顺序不被编译器优化,C#关键字volatile在语言层面提供了对易变构造的支持,标记为volatile的字段在按引用传递时无效。 13 public static class Volatile 14 { 15     public static void Write(ref Int32 location,Int32 value); 16     public static Int32 Read(ref Int32 location); 17 } 18  19 //互锁构造 20 public static class Interlocked 21 { 22     public static Int32 Exchange(ref Int32 location,Int32 value); 23  24     //Int32 old=location; if(location==comparand){ location=comparand;} return old; 25     public static Int32 CompareExchange(ref Int32 location,Int32 value,Int32 comparand); 26 } 27  28 //简单自旋锁:自旋会浪费CPU时间,因此自旋锁只适用于执行的非常快的代码区域。在单CPU计算机上,希望获得锁的线程会不断的自旋,如果获得锁的线程优先级比较低的话, 29 //会导致自旋的线程抢占CPU时间,从而影响拥有锁的线程释放锁,比较容易形成“活锁”。 30 public struct SimpleSpinlock 31 { 32     private Int32 _inUse;//0:false,1:true,Interlocked不支持Boolean 33     public void Enter() 34     { 35         while (true) 36         { 37             if(Interlocked.Exchange(ref _inUse,1)==0) 38                 return; 39             //一些其他代码 40         } 41     } 42     public void Leave() 43     { 44         Volatile.Write(ref _inUse,0); 45     } 46 } 47 public class Demo 48 { 49     private SimpleSpinlock _spinLock; 50     public void Access() 51     { 52         _spinLock.Enter(); 53         //取得锁,访问资源 54         _spinLock.Leave(); 55     } 56 } 57  58  59 //欢乐锁:用于完成一次原子性的操作,如果在执行过程中,数据被外部修改,那么当前执行的过程就无效,然后用修改后的值重新进行这次原子性的操作。 60 //说直白点就是乐观,与世无争。 61 public class OptimisticLoack 62 { 63     public delegate T Morpher<T, TArgument>(T startValue, TArgument argument); 64     public static T Morph<T, TArgument>(ref T target, TArgument value, Morpher<T,TArgument> morpher)where T:class 65     { 66         T currentValue =http://www.mamicode.com/ target, startValue, desiredValue; 67         do 68         { 69             startValue =http://www.mamicode.com/ currentValue; 70             desiredValue =http://www.mamicode.com/ morpher(startValue, value); 71             currentValue = http://www.mamicode.com/Interlocked.CompareExchange(ref target, desiredValue, startValue); 72         } while (currentValue != startValue); 73         return desiredValue; 74     } 75 } 76  77 //事件构造:自动重置事件 手动重置事件 78 public class EventWaitHandle:WaitHandle 79 { 80     public Boolean Set(); 81     public Boolean Reset(); 82 } 83 public class AutoResetEvent():EventWaitHandle 84 { 85     public AutoResetEvent(Boolean initialState); 86 } 87 public class ManualResetEvent():EventWaitHandle 88 { 89     public ManualResetEvent(Boolean initialState); 90 } 91  92 //信号量构造 93 public class Semaphore:WaitHandle 94 { 95     public Semaphore(Int32 initialCount,Int32 maxinumCount); 96     public Int32 Release(); 97     public Int32 Release(Int32 releaseCount); 98 } 99 100 //互斥体构造,内部维护一个递归计数,可以实现递归锁101 public sealed class Mutex:WaitHandle102 {103     public Mutex ();104     public void ReleaseMutex();105 }106 107 //事件构造,信号量构造,互斥体构造均可以简单的实现自旋锁108 public class SimpleSpinlock109 {110     private readonly Mutex m_lock=new Mutex();//针对互斥体构造111     //private readonly Semaphore m_lock=new Semaphore();//针对信号量构造112     //private readonly AutoResetEvent m_lock=new AutoResetEvent(true);//针对事件构造113     public void Enter()114     {115         m_lock.WaitOne();116     }117     public void Leave()118     {119         m_lock.ReleaseMutex();//针对互斥体构造120         //m_lock.Release(1);//针对信号量构造121         //m_lock.Set();//针对事件构造122     }123 }

 

[.net]基元线程同步构造