Synchronization constructs of four categories
- Simple blocking methods
- These wait for another thread to finish or for a period of time to elapse.
Sleep
,Join
, andTask.Wait
are simple blocking methods. - Locking constructs
- These limit the number of threads that can perform some activity or execute a section of code at a time.Exclusive locking constructs are most common — these allow just one thread in at a time, and allow competing threads to access common data without interfering with each other. The standard exclusive locking constructs are
lock
(Monitor.Enter
/Monitor.Exit
),Mutex
, and SpinLock. The nonexclusive locking constructs areSemaphore
,SemaphoreSlim
, and the reader/writer locks. - Signaling constructs
- These allow a thread to pause until receiving a notification from another, avoiding the need for inefficient polling. There are two commonly used signaling devices: event wait handles and
Monitor
’sWait/Pulse
methods. Framework 4.0 introduces theCountdownEvent
andBarrier
classes. - Nonblocking synchronization constructs
- These protect access to a common field by calling upon processor primitives. The CLR and C# provide the following nonblocking constructs:
Thread.MemoryBarrier, Thread.VolatileRead, Thread.VolatileWrite
, thevolatile
keyword, and theInterlocked
class.
Blocking is essential to all but the last category.
posted on 2012-06-03 20:39 malaikuangren 阅读(193) 评论(0) 编辑 收藏 举报