1

我需要使用 Interlocked 类处理我的 C# 应用程序中的锁。我有这样的代码:

class LogStruct
{
    public Dictionary<string, ulong> domainName;
    public Dictionary<string, ulong> URL;
    public Dictionary<string, ulong> domainData;
    public Dictionary<string, ulong> errorCodes;

    public LogStruct()
    {
        domainName = new Dictionary<string, ulong> { };
        URL = new Dictionary<string, ulong> { };
        domainData = new Dictionary<string, ulong> { };
        errorCodes = new Dictionary<string, ulong> { };
    }
}

class CLogParser
{
    string domainName = parameters[0];
    string errorCode = matches[1].Value;
    LogStruct m_logStruct;
    ...
    public CLogParser()
    {
         m_logStruct = new LogStruct();
    }
    ...
    public void ThreadProc(object param)
    {
      if (m_logStruct.errorCodes.ContainsKey(fullErrCode))
      {
        lock (m_logStruct.errorCodes)
        {
          m_logStruct.errorCodes[fullErrCode]++;
        }
      }
    }
}

当我想在 Interlocked 类上替换 ThreadProc 中的锁时,例如:

public void ThreadProc(object param)
{
  if (m_logStruct.errorCodes.ContainsKey(fullErrCode))
  {
    Interlocked.Increment(m_logStruct.errorCodes[fullErrCode]);
  }
}

我收到此错误:

Error CS1502: The best overloaded method match for 
`System.Threading.Interlocked.Increment(ref int)' 
has some invalid arguments (CS1502) (projectx)

这个错误:错误 CS1503: Argument #1' cannot convert ulong to ref int' (CS1503) (projectx)

如何解决?

4

1 回答 1

2

ref调用 Interlocked.Increment 时需要使用,例如

Interlocked.Increment(ref myLong);

或者在你的情况下

Interlocked.Increment(ref m_logStruct.errorCodes[fullErrCode]);

重要的是要意识到这Interlocked.Increment(ref long)是...

仅在 System.IntPtr 为 64 位长的系统上才是真正的原子。在其他系统上,这些方法相对于彼此是原子的,但相对于其他访问数据的方式而言不是原子的。因此,要在 32 位系统上实现线程安全,对 64 位值的任何访问都必须通过 Interlocked 类的成员进行。

http://msdn.microsoft.com/en-us/library/zs86dyzy(v=vs.110).aspx

附带说明一下,两者之间的实际性能差异

Interlocked.Increment(ref m_logStruct.errorCodes[fullErrCode]);

lock(myLock)
{
    m_logStruct.errorCodes[fullErrCode]++;
}

对于大多数应用程序来说,这将是微不足道且不重要的。

更新

看起来您的数据类型是无符号的。看看 Jon Skeet 使用无符号类型的 Interlocked.Increment 的解决方案:

https://stackoverflow.com/a/934677/141172

于 2014-10-16T22:04:30.027 回答