Lock vs. Mutex

  • Thread starter Thread starter Tim Johnson
  • Start date Start date
T

Tim Johnson

The synopsis for the lock statement indicates it uses a mutex object
internally, and it seems a lot simpler to use, as in:

lock(myObject) or lock(typeof(myStaticObject))
{
//locked statements here
}

Any comments on the pros/cons of the two approaches, in sharing access to a
common object from multiple threads?

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
The lock (SyncLock in VB) is equivalent to using a Monitor.Enter/Exit pair
(wrapped in a try finally) - ildasm to check it out.

If you want to map it to a native equivalent think of it as a critical
section... A mutex is more expensive than a critical section and is
generally more applicable for cross-process scenarios.

Cheers
Daniel
 
Back
Top