c # of thread synchronization - Synchronization Interlocked lightweight

Lightweight synchronization Interlock

Why it is lightweight it? Because it is only integer data (i.e. type int, long too) synchronization.

If you have studied the operating system inside the PV operation (eg semaphores), then you already know about it in general. It implements exactly like semaphore functions. Here's how it provides:

Interlocked.Increment(ref value) Value plus one (atomicity operation)
Interlocked.Decrement(ref value) Save a value (atomic operations)
Interlocked.Exchange(ref value1, value2) Exchange: assign the value 2 value 1; return the new value
Interlocked.CompareExchange(ref value1, value2, value3) Compare and swap to achieve two functions: the value 1 and the third comparison value, if the same, the value 2 to the value 1, no any operation is not the same; returns the original value (used for determining conditions) (Example 3 will be used)

Guess you like

Origin www.cnblogs.com/ryanzheng/p/10991045.html