Monitor vs Mutex in c#

C#MultithreadingSynchronization

C# Problem Overview


> Possible Duplicate:
> What are the differences between various threading synchronization options in C#?

What is the difference between a Monitor and a Mutex in C#?

When to use a Monitor and when to use a Mutex in C#?

C# Solutions


Solution 1 - C#

A Monitor is managed, and more lightweight - but is restricted to your AppDomain. A Mutex can be named, and can span processes (allowing some simple IPC scenarios between applications), and can be used in code that wants a wait-handle).

For most simple scenarios, Monitor (via lock) is fine.

Solution 2 - C#

A good source of advice on this stuff is the "Threading in C#" by Joseph Albahari. All the content is available online. In my opinion, it's worth to read the whole book, but yo can check these parts:

Although it does not cover .NET 4.0 new parallel constructs, it's a very good starting point.

Update: The book has been updated. Now, it covers .NET 4.0 Parallel Programming in its part 5.

Solution 3 - C#

A Mutex can be shared across processes, and is much more heavy-weight than a Monitor.

Use a Monitor unless you need to synchronize across process boundaries.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAjayView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#jpbochiView Answer on Stackoverflow
Solution 3 - C#Kim GräsmanView Answer on Stackoverflow