开发者

Lock,Monitor,Mutex [duplicate]

开发者_运维技巧This question already has answers here: Closed 13 years ago.

Possible Duplicate:

Monitor vs Mutex in c#

Hi,

In this site i found different different answers from different people which makes confusing. still not at all clear exactly on which scenario each of the three terms [Lock,Monitor,Mutex] will be very useful for the realtime need. Makes much more confusion between these terms.

I would require very clear differences in High level-in depth and which is essentially required to use among each of the three terms with better example and with clear understanding.

Kindly provide the info at the very high level of realtime usage or need using C#.NET with very good example.

Thanks Sukumar


Simplified and in short:

A Monitor is the managed .NET synchronization primitive (scope is one application domain only). The C# lock() does nothing but use Monitor and a try...finally clause to make sure that the lock is released in the case of an exception.

A Mutex is an OS synchronization object, which can also be used to synchronize across multiple processes (via named mutex).


  • A 'Lock' is a general term, could mean several things
  • the locks statement, lock(x) { } uses the Monitor class
  • the Monitor class is a (relatively) lightweight mutex class. It is fully build in managed code and needs no interaction with WIN32 API's
  • A Mutex is a Win32 class. Named Mutexes can be useful to sync across applications, but otherwise use Monitor.
  • You forgot ResetEvents and Waithandles
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜