1 /*Primitive thread synchronization constructs2 user mode constructs:3 variable structure (volatile Construct)4 interlocking Construction (interlocked Construct): Spin lock (Spinlock) optimistic lock (optimistic Concurrency control, optimistic concurrency control)5 kernel-mode constructs:6 event Constructs (events)7 Semaphore Construction (Semaphore)8 Mutex Construction (mutex)9 */Ten One //variable construction, Volatile.write () before all field write operations must be completed before the method call, Volatile.read () all fields before the read operation must be completed before the method, ensuring that the method functions in the field A //the assignment or reading order is not optimized by the compiler, C # keyword volatile provides support for variable constructs at the language level, and fields marked as volatile are not valid when passed by reference. - Public Static classVolatile - { the Public Static voidWrite (refInt32 location,int32 value); - Public StaticInt32 Read (refInt32 location); - } - + //Interlock Construction - Public Static classInterlocked + { A Public StaticInt32 Exchange (refInt32 location,int32 value); at - //Int32 old=location; if (Location==comparand) {Location=comparand;} return old; - Public StaticInt32 CompareExchange (refInt32 location,int32 value,int32 comparand); - } - - //Simple spin Lock: Spin is a waste of CPU time, so spin locks are only available for very fast code areas to execute. On a single-CPU computer, the thread that wants to get the lock spins constantly, and if the thread that gets the lock has a lower priority, in //causes the spin thread to preempt CPU time, which affects the thread that owns the lock to release the lock, which is easier to form a "live lock." - Public structSimplespinlock to { + PrivateInt32 _inuse;//0:false,1:true,interlocked does not support Boolean - Public voidEnter () the { * while(true) $ {Panax Notoginseng if(Interlocked.exchange (ref_inuse,1)==0) - return; the //Some other code + } A } the Public voidLeave () + { -Volatile.write (ref_inuse,0); $ } $ } - Public classDemo - { the PrivateSimplespinlock _spinlock; - Public voidAccess ()Wuyi { the _spinlock.enter (); - //get locks, access resources Wu _spinlock.leave (); - } About } $ - - //Happy lock: Used to complete an atomic operation, if the data is modified externally during execution, the current execution process is invalid, and then the atomic operation is re-performed with the modified value. - //the straightforward point is optimistic, aloof. A Public classOptimisticloack + { the Public DelegateT Morpher<t, targument>(T startvalue, targument argument); - Public StaticT Morph<t, Targument> (refT target, targument value, Morpher<t,targument> morpher)whereT:class $ { theT CurrentValue =Target, Startvalue, Desiredvalue; the Do the { theStartvalue =CurrentValue; -Desiredvalue =Morpher (startvalue, value); inCurrentValue = Interlocked.compareexchange (refTarget, Desiredvalue, startvalue); the} while(CurrentValue! =startvalue); the returnDesiredvalue; About } the } the the //Event constructs: Auto-reset events manually reset events + Public classEventwaithandle:waithandle - { the PublicBoolean Set ();Bayi PublicBoolean Reset (); the } the Public classAutoResetEvent (): EventWaitHandle - { - PublicAutoResetEvent (Boolean initialstate); the } the Public classManualResetEvent (): EventWaitHandle the { the PublicManualResetEvent (Boolean initialstate); - } the the //Semaphore Construction the Public classSemaphore:waithandle94 { the PublicSemaphore (Int32 initialcount,int32 maxinumcount); the PublicInt32 Release (); the PublicInt32 Release (Int32 releasecount);98 } About - //mutex construction, internal maintenance a recursive count, can implement recursive lock101 Public Sealed classMutex:waithandle102 {103 PublicMutex ();104 Public voidReleaseMutex (); the }106 107 //event Construction, semaphore construction, and mutex construction all can be easily realized by spin lock108 Public classSimplespinlock109 { the Private ReadOnlyMutex m_lock=NewMutex ();//for mutex construction111 //private readonly Semaphore m_lock=new Semaphore ();//for Semaphore Construction the //private readonly AutoResetEvent m_lock=new AutoResetEvent (true);//for event Construction113 Public voidEnter () the { the M_lock. WaitOne (); the }117 Public voidLeave ()118 {119M_lock. ReleaseMutex ();//for mutex construction - //M_lock. Release (1);//for Semaphore Construction121 //M_lock. Set ();//for event Construction122 }123}
[. NET] Primitive thread synchronization constructs