赋值给volatile变量在C# [英] Assignment to volatile variable in C#

查看:120
本文介绍了赋值给volatile变量在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#的理解说(感谢杰夫里氏&放大器;乔恩斯基特)的任务是原子。不就是当我们混读&安培;写(递增/递减),因此我们需要使用的联锁方法。如果只读取和放大器;分配将两者的操作是原子?

My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment is "atomic". What is not is when we mix reads & writes (increment/decrement) and hence we need to use methods on the Interlocked. If have only Read & assign would both the operations be atomic?

public class Xyz
{
    private volatile int _lastValue;
    private IList<int> AvailableValues { get; set; }
    private object syncRoot = new object();
    private Random random = new Random();

    //Accessible by multiple threads
    public int GetNextValue() //and return last value once store is exhausted
    {
        //...

        var count = 0;
        var returnValue = 0;

        lock (syncRoot)
        {
            count = AvailableValues.Count;
        }

        if (count == 0)
        {
            //Read... without locking... potential multiple reads
            returnValue = _lastValue;
        }
        else
        {

            var toReturn = random.Next(0, count);

            lock (syncRoot)
            {
                returnValue = AvailableValues[toReturn];
                AvailableValues.RemoveAt(toReturn);
            }
            //potential multiple writes... last writer wins
            _lastValue = returnValue;
         }

        return returnValue;

    }



推荐答案

挥发性其实更多是涉及到缓存(寄存器等);与挥发性您知道该值实际写入到/读从内存中的立即的(这实际上不是总是这样以其他方式)。这使得不同的线程立即看到彼此的更新。但不存在与指令重新排序等微妙的问题,但得到的复杂

volatile is actually more related to caching (in registers etc); with volatile you know that that value is actually written-to/read-from memory immediately (which isn't actually always the case otherwise). This allows different threads to immediately see updates from each other. There are other subtle issues with instruction re-ordering, but that gets complex.

有的原子两方面的含义在这里考虑:

There are two meanings of "atomic" to consider here:


  • 是一个单一的阅读本身原子/写本身的原子(即可能另一个线程得到两个双击<两个不同的半/ code> S,产生了许多从来没有真正存在的)

  • 是一个读/写对原子/隔离的一起

  • is a single read atomic by itself / write atomic by itself (i.e. could another thread get two different halves of two Doubles, yielding a number that never actually existed)
  • is a read/write pair atomic/isolated together

的本身依赖于值的大小;可以将其在单次操作来更新?读/写对是更多地与隔离 - 即防止丢失更新

The "by itself" depends on the size of the value; can it be updated in a single operation? The read/write pair is more to do with isolation - i.e. preventing lost updates.

在您的例子,它有可能为两个线程读取相同 _lastValue ,都做计算,然后(分别)更新 _lastValue 。其中一个更新会迷路。在现实中,我希望你想有一个锁定持续时间读/写过程。

In your example, it is possible for two threads to read the same _lastValue, both do the calculations, and then (separately) update _lastValue. One of those updates is going to get lost. In reality, I expect you want a lock over the duration of the read/write process.

这篇关于赋值给volatile变量在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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