C# 中各种线程同步选项有什么区别?

What are the differences between various threading synchronization options in C#?(C# 中各种线程同步选项有什么区别?)

本文介绍了C# 中各种线程同步选项有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下两者的区别:

Can someone explain the difference between:

  • 锁定(某个对象){}
  • 使用互斥锁
  • 使用信号量
  • 使用监视器
  • 使用其他 .Net 同步类

我就是想不通.在我看来,前两个是一样的?

I just can't figure it out. It seems to me the first two are the same?

推荐答案

好问题.我可能错了..让我试试..我的原始答案的修订版#2..有了更多的理解.感谢您让我阅读:)

Great question. I maybe wrong.. Let me try.. Revision#2 of my orig answer.. with a little bit of more understanding. Thanks for making me read :)

锁(obj)

  • 是一个用于(对象内?)线程同步的 CLR 构造.确保只有一个线程可以获取对象的锁的所有权 &输入锁定的代码块.其他线程必须等到当前所有者通过退出代码块来放弃锁定.此外,建议您锁定类的私有成员对象.

监视器

  • lock(obj) 是在内部使用 Monitor 实现的.您应该更喜欢 lock(obj),因为它可以防止您像忘记清理过程一样搞砸.如果您愿意,它是防白痴"的 Monitor 构造.
    使用 Monitor 通常比互斥锁更受欢迎,因为 Monitor 是专门为 .NET Framework 设计的,因此可以更好地利用资源.
  • lock(obj) is implemented internally using a Monitor. You should prefer lock(obj) because it prevents you from goofing up like forgetting the cleanup procedure. It 'idiot-proof's the Monitor construct if you will.
    Using Monitor is generally preferred over mutexes, because monitors were designed specifically for the .NET Framework and therefore make better use of resources.

使用锁或监视器对于防止同时执行对线程敏感的代码块很有用,但这些构造不允许一个线程将事件传递给另一个线程.这需要同步事件,它们是具有两种状态之一的对象,有信号和无信号,可用于激活和挂起线程.互斥量、信号量是操作系统级别的概念.例如,使用命名互斥锁,您可以跨多个(托管)exe 进行同步(确保您的应用程序只有一个实例在机器上运行.)

Using a lock or monitor is useful for preventing the simultaneous execution of thread-sensitive blocks of code, but these constructs do not allow one thread to communicate an event to another. This requires synchronization events, which are objects that have one of two states, signaled and un-signaled, that can be used to activate and suspend threads. Mutex, Semaphores are OS-level concepts. e.g with a named mutex you could synchronize across multiple (managed) exes (ensuring that only one instance of your application is running on the machine.)

互斥:

  • 然而,与监视器不同的是,互斥锁可用于跨进程同步线程.当用于进程间同步时,互斥锁被称为命名互斥锁,因为它将在另一个应用程序中使用,因此不能通过全局或静态变量共享.必须给它一个名称,以便两个应用程序都可以访问同一个互斥对象.相比之下,Mutex 类是 Win32 构造的包装器.虽然它比监视器更强大,但互斥体需要的互操作转换比 Monitor 类所需的计算成本更高.
  • Unlike monitors, however, a mutex can be used to synchronize threads across processes. When used for inter-process synchronization, a mutex is called a named mutex because it is to be used in another application, and therefore it cannot be shared by means of a global or static variable. It must be given a name so that both applications can access the same mutex object. In contrast, the Mutex class is a wrapper to a Win32 construct. While it is more powerful than a monitor, a mutex requires interop transitions that are more computationally expensive than those required by the Monitor class.

信号量(伤了我的大脑).

Semaphores (hurt my brain).

  • 使用 Semaphore 类来控制对资源池的访问.线程通过调用WaitOne方法进入信号量,该方法继承自WaitHandle类,并通过调用Release方法释放信号量.每次线程进入信号量时,信号量上的计数都会减少,而当线程释放信号量时,计数会增加.当计数为零时,后续请求会阻塞,直到其他线程释放信号量.当所有线程都释放信号量时,计数为创建信号量时指定的最大值.一个线程可以多次进入信号量..Semaphore 类不强制 WaitOne 或 Release 上的线程标识..程序员有责任不搞砸.信号量有两种类型:本地信号量和命名系统信号量.如果您使用接受名称的构造函数创建 Semaphore 对象,则它与该名称的操作系统信号量相关联.命名系统信号量在整个操作系统中都是可见的,并且可用于同步过程.本地信号量仅存在于您的进程中.它可以被进程中任何引用本地信号量对象的线程使用.每个 Semaphore 对象都是一个单独的本地信号量.
  • Use the Semaphore class to control access to a pool of resources. Threads enter the semaphore by calling the WaitOne method, which is inherited from the WaitHandle class, and release the semaphore by calling the Release method. The count on a semaphore is decremented each time a thread enters the semaphore, and incremented when a thread releases the semaphore. When the count is zero, subsequent requests block until other threads release the semaphore. When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created. A thread can enter the semaphore multiple times..The Semaphore class does not enforce thread identity on WaitOne or Release.. programmers responsibility to not muck up. Semaphores are of two types: local semaphores and named system semaphores. If you create a Semaphore object using a constructor that accepts a name, it is associated with an operating-system semaphore of that name. Named system semaphores are visible throughout the operating system, and can be used to synchronize the activities of processes. A local semaphore exists only within your process. It can be used by any thread in your process that has a reference to the local Semaphore object. Each Semaphore object is a separate local semaphore.

要阅读的页面 - 线程同步 (C#)

这篇关于C# 中各种线程同步选项有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C# 中各种线程同步选项有什么区别?

基础教程推荐