cj said:
I take it a mutex is like a synclock but visible outside the program?
Yea, that's a pretty good working definition.
There are some differences, but at the end of the day they're both
True/False flags for "Busy / Not Busy".
The Monitor (the thing that underlies the SyncLock) has some nice methods on
it like TryEnter and PulseAll that make it easy to use in some situtations.
It also has the handy VB keyword SyncLock that deals with the Try/Finally
semantics required to use a Monitor in an Exception safe way.
On the other hand, the Mutex is also a WaitHandle, and therefore works with
all of that infrastructure (WaitOne, WaitAll, WaitAny, etc), which makes it
easy to use in some situations. For example, you can register a WaitHandle
with the CLR threadpool, and get a callback when the WaitHandle is set.
There are some scenarios where this behavior is very handy.
Personally, I tend to use:
- Monitor (Synclock) for almost all of my locking.
- ManualResetEvents for almost all of my Waiting ("Is this done yet? Call me
back when it's done.")
- Mutex for ensuring an application is "single instance".