Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The following 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?

like image 736
Jader Dias Avatar asked Jan 08 '09 17:01

Jader Dias


4 Answers

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

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.

like image 141
Darin Dimitrov Avatar answered Oct 19 '22 17:10

Darin Dimitrov


Basically the warning is that when you pass a volatile field by reference, the calling code doesn't know to treat it in a volatile manner. For Interlocked.Increment that probably doesn't matter, due to the nature of the method - but then you don't need the variable to be volatile anyway if you're using Interlocked.

In general, I think I'd avoid mixing the two - if you're using Interlocked, do it everywhere (using Interlocked.CompareExchange(ref counter, 0, 0) to read it). I can't say I use volatile very often, personally. For simple counters I might use Interlocked, but I'm more likely to use a lock for most tasks.

like image 26
Jon Skeet Avatar answered Oct 19 '22 19:10

Jon Skeet


Use this:

#pragma warning disable 420
if(Interlocked.CompareExchange(ref isLoaded, 1, 0) != 0)
    return;
#pragma warning restore 420
like image 31
Robert Fraser Avatar answered Oct 19 '22 17:10

Robert Fraser


You're getting the error because you're passing the field by reference. I think what this means is that the target method has no idea the field is marked as volatile, and therefore will not treat it as such.

like image 3
Kent Boogaart Avatar answered Oct 19 '22 19:10

Kent Boogaart