什么是C#各种线程同步选项之间的区别是什么? [英] What are the differences between various threading synchronization options in C#?

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

问题描述

一个人能解释的区别:


  • 锁(someobject){}

  • 使用互斥

  • 使用旗语

  • 使用监视器

  • 使用其他.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构造的(内部对象?)线程同步。确保只有一个线程可以把对象的锁和放大器的所有权;输入code的锁定块。其他线程必须等待,直到当前的所有者通过退出code块放弃锁。同时建议您锁定类的私有成员对象。

显示器


  • 锁(OBJ)在内部使用显示器来实现。的你应该preFER锁(OBJ),因为它prevents你喜欢忘记清理过程消磨了。这是傻瓜型的显示器构造,如果你会。

    使用监视器一般是$ pferred超过互斥p $,因为显示器是为.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.

使用一个锁或监视器为preventing $ C $的c螺纹敏感块的同时执行有用的,但<强>这些构建不允许一个线程对事件进行通信到另一个。这需要同步事件,它是具有两个状态的一个信号和未发出信号的对象,可以用来激活和挂起线程。
互斥,信号量是操作系统级的概念。例如用命名的互斥体,你可以在多个同步(管理)的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.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx\">Semaphores (伤害我的大脑)。

Semaphores (hurt my brain).


  • 使用Semaphore类控制对资源池。线程通过调用WaitOne的方法,这是从WaitHandle的类继承输入信号,并通过调用Release方法释放信号量。
    上的信号的计数,每当一个线程进入信号时递减,并且当一个线程释放信号量增加。当计数为零时,后续请求被阻塞,直到其他线程释放信号量。当所有线程都释放信号,计数是创建信号灯时指定的最大值。
    线程可以进入信号量多times..The Semaphore类的WaitOne的不强制线程标识或发行..程序员的责任不弄脏。
    信号灯有两种类型:局部信号量和命名的系统信号灯。如果您使用接受名称的构造函数创建一个信号量对象,它与该名称的操作系统信号量相关联。命名系统信号量是整个操作系统中可见,并且可用于活动的同步流程。
    本地信号仅存的过程。它可以通过在过程中的任何线程前往当地的信号灯对象的引用来使用。每个信号量对象是一个单独的本地信号。

  • 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#各种线程同步选项之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆