Lock,Monitor,Mutex [duplicate]
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
精彩评论