English 中文(简体)
是否应当使用“雷莫斯”模式?
原标题:Should the Double Check Lock pattern be used with ReaderWriteLockSlim?

是否有必要使用的<24检查。

Consider this scenario: I have a dictionary. Things can be added to it. But things can t be removed from it. When things are added, it can be a very expensive operation in terms of time (only hundreds of milliseconds, but still costly relative to the rest of the app) If I wanted to add something and it wasn t already there, would there be anything to gain by:

  1. first acquiring a read lock, then checking for existence,
  2. then entering an upgradable read lock, and checking again,
  3. then entering a write lock if the item is still not present in the dictionary?

类似于:

void populateIfNotPresent( object thing )
{
        _lock.EnterReadLock( ) ;

        bool there = _dictionary.ContainsKey(thing);

        _lock.ExitReadLock( ) ;

        // Remember, the specs say nothing can be removed from this dictionary.
        if (!there)
        {
            _lock.EnterUpgradeableReadLock( ) ;

            try
            {
                if( !_dictionary.ContainsKey( thing ) )
                {
                    _lock.EnterWriteLock( ) ;
                    try
                    {
                        populate( thing ) ;
                    }
                    finally
                    {
                        _lock.ExitWriteLock( ) ;
                    }
                }
            }
            finally
            {
                _lock.ExitUpgradeableReadLock( ) ;
            }
        }
}

当时,只有一只读物可以进入一个可升级的24小时,但不会阻止任何其他的read子进入一个读24小时,因此,似乎在双重检查锁上有价值

你认为什么? 难道这种高技能吗?

最佳回答
问题回答

因此,双重检查似乎有价值。

价值多少? 如果你想看到许多海滩,那么,可以发挥可加固的锁子的作用;然而,如果你回,而不是,期望看到大量海滩,那么你会再做不必要的锁。 总的来说,我要谈谈找到工作的最简单的解决办法。 优化锁,通常不是你会为你的面包 the获得最大的香料,而是寻找更多的东西,首先优化。

Suggestion:
Something that might give you a lot more bang for your buck is a Striped Dictionary (
Java s StripedMap is a pretty good place to start and it shouldn t be very hard to understand).

<代码>的基本概念 缩略语 你们有一系列的锁:

object[] syncs = new object[n]();
// also create n new objects

你们应该用足够多的脱光线来脱下你的地图,以便允许你在不相撞的情况下进入该方法的深层。 我没有任何数据可以证实这一点,但恳请你们再看着多达8条read子进入地图,那么你可能使用8个或更多的锁,以确保所有8个read子能够同时进入地图。 如果你想要更好的“保险”来对付“col”,那么就会产生更多的 strip,即32或64。

当你进入<代码>populateIfNotpresent方法时,你根据散页码锁在其中一锁上:

void populateIfNotPresent( object thing )
{
    lock(syncs[thing.GetHashCode()%syncs.Length])
    {
        if(!dictionary.ContainsKey(thing))
        {
            populate(thing);
        }
    }
}

Suppose You have 8 artes, now You re granted up to 8 threads to safe into and do an cost operation, which would have otherwise prevent the other 7 threads. 当然,假设洗衣功能足够坚固,可以减少重复的可能性。

您已经期望<代码>populateIf notpresent 昂贵,但是如果你有一条脱节的字典,那么你可以就各个字典不同部门进行多方面的校对工作,而不相互影响。 这将给你带来更大的好处,使万国邮联的多个周期从检查是否存在物体时起步,因为昂贵的操作是在标的<>印有<时。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...