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

前端 未结 4 1431
慢半拍i
慢半拍i 2020-12-02 15:52

The following code

using System.Threading;

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


        
4条回答
  •  鱼传尺愫
    2020-12-02 16:01

    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.

提交回复
热议问题