一些澄清SyncRoot上的模式:什么是使用这种模式的正确方法? [英] Some clarification on the SyncRoot pattern: what is the correct way to use this pattern?

查看:270
本文介绍了一些澄清SyncRoot上的模式:什么是使用这种模式的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的一些有关的 SyncRoot上图案作为一般规则,以避免死锁。而前读了几年的问题(见本链接) ,我想我明白,这种模式的某些应用可能不正确。尤其是,我把重点放在下面的句子从这个话题< /一>:

I read something about the SyncRoot pattern as a general rule to avoid deadlocks. And reading a question of a few years ago (see this link), I think I understand that some uses of this pattern may be incorrect. In particular, I focused on the following sentences from this topic:

你会发现System.Collections中在许多的正宗SyncRoot属性。在retrospeced,我觉得这个属性是一个   错误...请放心,我们不会再犯同样的错误,我们建立   这些集合的仿制版本。

You’ll notice a SyncRoot property on many of the Collections in System.Collections. In retrospeced, I think this property was a mistake... Rest assured we will not make the same mistake as we build the generic versions of these collections.

事实上,例如,名单,其中,T&GT; 类不是工具 SyncRoot上属性,或者更正确呢它是明确的实施(见这个答案),所以你必须转换为的ICollection 以使用它。但是,<一个href="http://stackoverflow.com/questions/728896/whats-the-use-of-the-syncroot-pattern#comment13873399_728934">this评论认为,让私人 SyncRoot上业界人士一样糟糕的做法是锁定在(见< A HREF =htt​​p://stackoverflow.com/questions/251391/why-is-lockthis-bad>这个答案),如也证实了<一href="http://stackoverflow.com/questions/3910628/any-downsides-to-locking-a-collection-vs-a-syncroot#comment4168873_3910716">this评论。

In fact, for example, List<T> class doesn't implements SyncRoot property, or more correctly it is implemented explicitly (see this answer), so you must cast to ICollection in order to use it. But this comment argues that making a private SyncRoot field public is as bad practice as locking on this (see this answer), as also confirmed in this comment.

所以,如果我理解正确的话,当我实现了一个非线程安全的数据结构,因为它可以在多线程环境中使用,我不应该(实际上,我不能)提供 SyncRoot上属性。但我要离开开发(会使用这种数据结构是谁)与在下面的示例code也有私人SyncRoot上对象相关联的任务。

So, if I understand correctly, when I implement a non thread-safe data structure, since it could be used in a multithreaded context, I should not (actually, I must not) provide the SyncRoot property. But I should leave the developer (who will use this data structure) with the task of associating it with a private SyncRoot object as in the following sample code.

public class A
{
    private MyNonThreadSafeDataStructure list;
    private readonly object list_SyncRoot = new object;

    public Method1()
    {
        lock(list_SyncRoot)
        {
            // access to "list" private field
        }
    }

    public Method2()
    {
        lock(list_SyncRoot)
        {
            // access to "list" private field
        }
    }
}

在总之,我理解,对于同步/锁定的最佳实践应该如下:

In summary, I understood that the best practices for synchronization/locking should be as follows:

  1. 在任何私人SyncRoot上的对象不应该通过公共财产暴露;换言之,自定义数据结构不应该提供一个公共 SyncRoot上属性(也见<一href="http://stackoverflow.com/questions/6525118/why-lock-on-collection-syncroot-instead-of-just-lock-the-collection#comment7682030_6525218">this评论)。
  2. 在一般情况下,它不是强制使用专用对象锁定(见这个答案)。
  3. 如果一个类具有多组需要被同步,但不与对方操作的,它应该有多个专用SyncRoot上的对象(见<一href="http://stackoverflow.com/questions/3910628/any-downsides-to-locking-a-collection-vs-a-syncroot#comment4172092_3910656">this评论)。
  1. Any private SyncRoot objects should not be exposed through a public property; in other words, a custom data structure should not provide a public SyncRoot property (see also this comment).
  2. In general, it is not mandatory to use private objects for locking (see this answer).
  3. If a class has multiple sets of operations that need to be synchronised, but not with each other, it should have multiple private SyncRoot objects (see this comment).

什么是上面写的正确使用这种模式?

Is what written above the proper use of this pattern?

推荐答案

我将避免加入 SyncRoot上属性设置为我设计的类型,这里的原因:

I would avoid adding a SyncRoot property to the type that I design, here are the reasons:

该类型将成为胖:责任变得更加分散。为什么我添加了明确的多线程锁定以及其他绒毛不支持支持?我将迫使用户遵循仅一个实践中,这可能不是在所有情况下的最佳解决方案

The type becomes fatter: its responsibility becomes more scattered. Why would I add support for explicit multithreading locking and no support for other fluff? I would force the user to follow only one practise, which may not be the best solution in all cases

我需要正确执行的财产(不返回的typeof(MyClass的)) ,即这是错误的:

I would need to implement the property correctly (no returning this or typeof(MyClass)), i.e. this is wrong:

public object SyncRoot {get {return this;}}

我也想避免使用 SyncRoot上属性的.NET Framework类型。如果我需要做一个类型的w / o SyncRoot上属性线程我会用一个锁定模式,如果一个类型都有这个属性我仍然不选择上锁定 SyncRoot上。这使我的code风格一致,更容易阅读/维护。

I would also avoid using SyncRoot property from the .NET framework types. If I need to make a type w/o SyncRoot property threadsafe I will use one locking pattern, and if a type has this property I will still not opt for locking on SyncRoot. This makes my code style consistent and easier to read/maintain.

这篇关于一些澄清SyncRoot上的模式:什么是使用这种模式的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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