"引用volatile字段将不会被视为挥发性"影响 [英] "A reference to a volatile field will not be treated as volatile" implications

查看:403
本文介绍了"引用volatile字段将不会被视为挥发性"影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code

using System.Threading;

class Test
{
    volatile int counter = 0;
    public void Increment()
    {
    	Interlocked.Increment(ref counter);
    }
}

引发以下编译器警告:

Raises the following compiler warning:

"A reference to a volatile field will not be treated as volatile"

我是不是做错了什么在这里提出这个警告呢?为什么编译器我提醒这个呢?

Am I doing something wrong here to raise this warning? Why does the compiler me warn about this?

推荐答案

您没有做错什么。按照文档

You are not doing anything wrong. According to the documentation:

一个volatile字段通常不应   使用ref或out传递   参数,因为它不会被   视为范围内的挥发性   的功能。但也有例外   于此,调用一个时,如   互锁API。

A volatile field should not normally be passed using a ref or out parameter, since it will not be treated as volatile within the scope of the function. There are exceptions to this, such as when calling an interlocked API.

这篇关于"引用volatile字段将不会被视为挥发性"影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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