简体   繁体   English

C#volatile数组项?

[英]C# volatile array items?

I need an array with volatile items, and can't find a way to do that. 我需要一个包含volatile项的数组,并且找不到这样做的方法。

private volatile T[] _arr;

This means that the _arr reference is volatile, however it does not guarantee anything about the items inside the _arr object itself. 这意味着_arr引用是易失性的,但它不保证_arr对象本身内部的任何内容。

Is there any way to mark the _arr's Items as volatile? 有没有办法将_arr的Items标记为volatile?

Thanks. 谢谢。

EDIT: 编辑:

The following code built according to binarycoder's answer. 以下代码根据二进制编码的答案构建。 Is this code thread-safe to use? 这段代码是否可以使用线程安全?

public class VolatileArray<T>
{
    private T[] _arr;

    public VolatileArray(int length)
    {
        _arr = new T[length];
    }

    public VolatileArray(T[] arr)
    {
        _arr = arr;
    }

    public T this[int index]
    {
        get
        {
            T value = _arr[index];
            Thread.MemoryBarrier();
            return value;
        }

        set
        {
            Thread.MemoryBarrier();
            _arr[index] = value;
        }
    }

    public int Length
    {
        get { return _arr.Length; }
    }
}

Since it is possible to pass array elements by reference, you can use Thread.VolatileRead and Thread.VolatileWrite . 由于可以通过引用传递数组元素,因此可以使用Thread.VolatileReadThread.VolatileWrite

It is useful to understand that the volatile keyword works behind the scenes by using Thread.MemoryBarrier . 通过使用Thread.MemoryBarrier来理解volatile关键字在幕后工作是很有用的。 You could write: 你可以写:

// Read
x = _arr[i];
Thread.MemoryBarrier();

// Write
Thread.MemoryBarrier();
_arr[i] = x;

Note that volatile and MemoryBarrier are advanced techniques that are both easy to get wrong. 请注意, volatileMemoryBarrier是易于出错的高级技术。 For example, see How do I Understand Read Memory Barriers and Volatile . 例如,请参阅如何理解读取内存屏障和易失性 Usually you are better off with higher level constructs such as lock , Monitor , ReaderWriterLockSlim , and others. 通常情况下,使用更高级别的构造(例如lockMonitorReaderWriterLockSlim等)会更好。

Use Volatile.Read(ref array[index]) and Volatile.Write(ref array[index], value) . 使用Volatile.Read(ref array[index])Volatile.Write(ref array[index], value)

Class Volatile is available since .NET 4.5. 从.NET 4.5开始,可以使用Volatile类。 It allows to read/write from/to fields, array elements, ref parameters, pointers. 它允许读/写字段,数组元素, ref参数,指针。

I made a little struct that helps keep things clean and OO 我做了一个小结构,有助于保持清洁和OO

struct VolatileBoolean {
    public volatile Boolean Value;
}

VolatileBoolean[] arrayOfVolatileBooleans;
public void SomeMethod() {
    if (arrayOfVolatileBooleans[4].Value)
        DoSomething();
}

I don't think that you can 我认为你不能

You can't, volatile is defined as a field-modifier (ECMA 334). 你不能, volatile被定义为一个字段修饰符(ECMA 334)。

And I don't think it will accomplish what you want either. 而且我认为它也不会达到你想要的效果。
Consider: 考虑:

 private T[] _arr;

 volatile T v;
 ....  v = _arr[x];
 ....  _arr[x] = v;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM