小编use*_*928的帖子

C# 易失性读取行为

在C#.net ConcurrentDictionary(C#参考源)的参考源代码中,我不明白为什么在以下代码片段中需要进行易失性读取:

public bool TryGetValue(TKey key, out TValue value)
{
    if (key == null) throw new ArgumentNullException("key");
      int bucketNo, lockNoUnused;

    // We must capture the m_buckets field in a local variable. 
    It is set to a new table on each table resize.
    Tables tables = m_tables;
    IEqualityComparer<TKey> comparer = tables.m_comparer;
    GetBucketAndLockNo(comparer.GetHashCode(key), 
                      out bucketNo, 
                      out lockNoUnused,
                      tables.m_buckets.Length,
                      tables.m_locks.Length);

    // We can get away w/out a lock here.
    // The Volatile.Read ensures that the load of the fields of 'n' …
Run Code Online (Sandbox Code Playgroud)

c# volatile memory-model

5
推荐指数
1
解决办法
435
查看次数

标签 统计

c# ×1

memory-model ×1

volatile ×1