在 C# 中监控与互斥锁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1164038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:44:46  来源:igfitidea点击:

Monitor vs Mutex in c#

c#multithreadingsynchronization

提问by Ajay

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

可能的重复:
C# 中各种线程同步选项之间有什么区别?

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

C# 中的 Monitor 和 Mutex 有什么区别?

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

在 C# 中何时使用监视器以及何时使用互斥锁?

回答by Kim Gr?sman

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

Mutex 可以跨进程共享,并且比 Monitor 更重要。

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

除非您需要跨进程边界进行同步,否则请使用监视器。

回答by Marc Gravell

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

AMonitor是托管的,并且更轻量级 - 但仅限于您的AppDomain. AMutex可以命名,并且可以跨越进程(允许在应用程序之间进行一些简单的 IPC 场景),并且可以在需要等待句柄的代码中使用。

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

对于大多数简单的场景,Monitor(via lock) 很好。

回答by jpbochi

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:

Joseph Albahari 撰写的“C# 中的线程”是有关这些内容的一个很好的建议来源。所有内容均可在线获取。在我看来,值得阅读整本书,但您可以检查以下部分:

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

虽然它不包括.NET 4.0 新的并行结构,但它是一个很好的起点。

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

更新:本书已更新。现在,它在第5 部分中介绍了 .NET 4.0 并行编程。