Lock vs. Mutex

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
 
D

Daniel Moth

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top