C#中锁定资源的更有效方法 [英] More efficient way of locking resources in C#

查看:320
本文介绍了C#中锁定资源的更有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我来这里的目的是关于多线程环境中资源锁定的效率.

在我的课堂上,我有词典资源,并且许多方法都针对它执行不同的操作.
为了提供线程安全,我添加了一个名为_RWLock的ReaderWriterLockSlim成员.
所以问题包括下一个,我有一个方法,请参见下文:

Hi guys, i came here with one question about efficiency in locking of resource in multi-threading enviroment.

In my class i have a dictionary resource and many methods wich performs differ actions against it.
For providing thread-safety i added a ReaderWriterLockSlim member called _RWLock.
So question consists of next, i''ve got a method, see below:

try
            {
                _RWLocker.EnterReadLock();
                if (_GameSessionsBankroll.ContainsKey(gameSessionId))
                {
                    long previousBankrol = _GameSessionsBankroll[gameSessionId];
                    try
                    {
                        _RWLocker.EnterWriteLock();
                        _GameSessionsBankroll[gameSessionId] = previousBankrol - amount;
                    }
                    finally
                    {
                        _RWLocker.ExitWriteLock();
                    }
                }
                else
                {
                    try
                    {
                        _RWLocker.EnterWriteLock();
                        _GameSessionsBankroll[gameSessionId] = amount;
                    }
                    finally
                    {
                        _RWLocker.ExitWriteLock();
                    }
                }
            }
            finally
            {
                _RWLocker.ExitReadLock();
            }




在不同的情况下同时写入和读取数据的情况下,线程安全是否正确实现?

还是我只能受单一锁定的限制?
出于性能考虑,任何人都可以向我说明哪种方法更有效吗?




Is it correct implementation of thread-safety in case of simultaneous writting and reading data amoung differ thraeds??

Or i just can be bounded by single locking ??
Can anybody explein me which way more efficient in case of performance consideration??

推荐答案

这取决于如何使用锁定的资源.读写锁比常规锁有一些开销,因此,如果您遇到这种情况,即对大多数这段代码的调用最终仅获得只读访问权,而只有很少的调用获得写访问权,则可以提高性能.因此,典型的情况是这样的:一些线程传递仅具有读取访问权限的锁,读取一些数据,然后根据本地线程数据和刚刚读取的数据来计算是否需要写访问权限.然后,该线程被提升到数据的写访问和写操作,与没有写访问权限相比,它或多或少地很少发生.

如果情况不同,或者不确定是否是这种情况,则使用常规锁可能会更好.

我认为您了解真正的吞吐量取决于许多因素,首先,取决于您的一般多线程代码设计.主要规则是:最好的锁是无锁".通过在堆栈上执行大多数进程(尤其是避免使用不必要的静态对象),不共享资源或将共享减少到绝对最小值,可以大大减少相互锁定.在某些情况下,可以通过传递数据引发中间传送带/队列来提高性能.即使您使用最快的锁定机制,也可以通过通用代码设计的糟糕体系结构将其弄糟.

—SA
It depends on how to use the locked resources. The reader-writer lock should have some overhead over the regular lock, so you can gain performance if you have the situation when most calls to this fragment of code end up getting only read-only access, and considerably fewer calls get write access. So, the typical scenario is this: some thread passes throw the lock with read access only, read some of data and, depending on the local thread data and the data just read, calculates if write access is needed. Then the thread is promoted to the write access and writes of data, and it happens more or less more rarely than without the write access.

If the scenario is different, or it is uncertain if this is the case or not, using a regular lock could be better.

I think you understand the real throughput depends on many factors, first of all, on you general multithreading code design. The main rule is: "the best lock is no lock". You can greatly reduce mutual locking by doing most processes on stack (avoiding unnecessary static objects, in particular), not sharing resources or reducing the sharing to absolute minimum. In certain cases you can improve performance by passing data throw intermediate conveyors/queues. Even if you use the fastest possible locking mechanism, you can screw it up by lousy architecture of general code design.

—SA


我认为一次使用写锁是正确的,因为您一直在进行写操作.试试( unested ):
I think a single use of the Write Lock is correct, as you are always doing a write. Try (untested):
try
{
  _RWLocker.EnterWriteLock();
  long previousBankrol;
  if (_GameSessionsBankroll.TryGetValue(gameSessionId, out previousBankrol))
  {
    _GameSessionsBankroll[gameSessionId] = previousBankrol - amount;
  }
  else
  {
    _GameSessionsBankroll[gameSessionId] = amount;
  }
}
finally
{
  _RWLocker.ExitWriteLock();
}


一个非常好的关于线程的免费参考,其中包括锁定是 http://www.albahari.com/threading/ [^ ] .立即下载,这是一个很好的参考.
A really good free reference on Threading, which includes locking is http://www.albahari.com/threading/[^]. Download it now, it is a great reference.


这篇关于C#中锁定资源的更有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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