首页 > 代码库 > C# 同步
C# 同步
- Interlocked
simplearithmetic operations are not atomic.Interlocked类提供了一个专门的机制用于完成这些特定的操作。这个类提供了Increment、Decrement、Add静态方法用于对int或long型变量的递增、递减或相加操作
Interlocked.Increment(ref counter);
AutoResetEvent
One simple, and thread-safe way to force a thread to wait until another is completed is to use the AutoResetEvent class
Wait将阻塞线程AutoResetEvent waitHandle = new AutoResetEvent(false);Thread t = new Thread(...);t.Start();// Wait (with timeout) here until you are notified!waitHandle.WaitOne(2000);Console.WriteLine("Other thread is done!");
// in thread t// Tell other thread we are done.waitHandle.Set();同步事件有两种:AutoResetEvent和 ManualResetEvent。它们之间唯一不同的地方就是在激活线程之后,状态是否自动由终止变为非终止。AutoResetEvent自动变为 no-signal. AutoResetEvent只能激活一个线程。而ManualResetEvent要等到它的Reset方法被调用,状态才变为no-signal,在这之前,ManualResetEvent可以激活任意多个线程。
C# lock Keyword
This keyword allows you to define a scope of statements that must be synchronized between thread
MSDN:最佳做法是定义 private 对象来锁定, 或 private static 对象变量来保护所有实例所共有的数据。// Lock token.private object threadLock = new object();// Use the private object lock token.lock (threadLock){ ...
// All code within this scope is thread safe.
}- Monitor
the lock keyword is equals to Monitor.Enter(threadLock, ref lockTaken);try{ ... }finally{ if (lockTaken) Monitor.Exit(threadLock);}
让当前线程在等待1000秒后根据返回的bool值来决定是否继续下面的操作
bool gotLock = Monitor.TryEnter(threadLock,1000)
Monitor.Pulse/wait:
|- 拥有锁的线程lockObj -> |- 就绪队列(ready queue) |- 等待队列(wait queue)
User Wait to go to the wait queue, use Pulse/PulseAll to push (all) from wait queue to ready queue
- [Synchronization] Attribute
In essence, this class-level attribute effectively locks down all instance member code of the object for thread safety.public class SynchronizationClass : ContextBoundObject { ... }
- [MethodImpl(MethodImplOptions.Synchronized)]
如果临界区是跨越整个方法的,也就是说,整个方法内部的代码都需要上锁的话,使用MethodImplAttribute属性会更简单一些. - ReaderWriterLock
C#提供了System.Threading.ReaderWriterLock类以适应多用户读/单用户写的场景。private static ReaderWriterLock m_readerWriterLock = new ReaderWriterLock();m_readerWriterLock.AcquireReaderLock(10000); ... m_readerWriterLock.ReleaseReaderLock();m_readerWriterLock.AcquireWriterLock(1000);...m_readerWriterLock.ReleaseWriterLock();
m_readerWriterLock.UpgradeToWriterLock(1000);