易失性变量和非易失性重新排序/可见性 [英] Volatile variable and non volatile reordering / visibility

查看:154
本文介绍了易失性变量和非易失性重新排序/可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我以为我对这门知识很了解,直到我读了一些东西,使我怀疑我对这一主题的知识.我几乎可以肯定这本书是不正确的,但也想问一下社区.

PS:尚未看到这本书的勘误表,因此很可能会被公开为错误.

一个简化的示例:

public class VolatileMain {

private volatile int a = 0;
private String text = "";

public static void main(String[] args) throws Exception {

    VolatileMain vm = new VolatileMain();

    Thread writer = new Thread() {

        @Override
        public void run() {
            System.out.println("Running thread " + Thread.currentThread().getName());
            vm.text = "hello world";
            vm.a = 5;
        }
    };

    writer.start();
    writer.join();

    System.out.println("Running thread " + Thread.currentThread().getName());
    System.out.println(vm.a);
    System.out.println(vm.text);

   }

}

因此,以给出的示例为前提,是否可以保证其他任何读取该线程的线程都可以保证线程编写器对文本"的写入是可见的?

作者似乎很支持变量"a"的易变语义,并确保当刷新"a"时也刷新对"text"的写操作,这是否可以保证?

我认为不是,但是我自己的快速测试(上面)与此相反

您的想法.

解决方案

假定由线程编写器对文本"的写入被保证可以被任何其他读取它的线程看到,这是正确的吗?

不.但是可以保证,在读取text之前,任何其他读取a的线程都可以看到它,就像您的示例所做的那样:

  • text的写操作发生在写线程中对a的写操作之前
  • 在主线程中读取a之前发生在写入器中写入a
  • 事前发生关系是可传递的
  • 因此,text的写入发生在读取a之前.

So I thought I knew this stuff well enough, until I read something which got me doubting my knowledge on this subject matter. I am almost certain the book is incorrect but would like to ask the community as well.

PS: Have not seen the errata of the book so could well be disclosed as an error.

A simplified example:

public class VolatileMain {

private volatile int a = 0;
private String text = "";

public static void main(String[] args) throws Exception {

    VolatileMain vm = new VolatileMain();

    Thread writer = new Thread() {

        @Override
        public void run() {
            System.out.println("Running thread " + Thread.currentThread().getName());
            vm.text = "hello world";
            vm.a = 5;
        }
    };

    writer.start();
    writer.join();

    System.out.println("Running thread " + Thread.currentThread().getName());
    System.out.println(vm.a);
    System.out.println(vm.text);

   }

}

So given the example is it correct to assume that the write to "text" by Thread writer is guaranteed to be visible by any other thread that reads it?

It seems the author is piggy backing on the volatile semantics of the variable "a" and ensuring that the write to "text" will also be flushed when "a" is flushed, is this a guarantee?

I didn't think it was, but my own quick test (above) to the contrary

Your thoughts.

解决方案

is it correct to assume that the write to "text" by Thread writer is guaranteed to be visible by any other thread that reads it?

No. But it's guaranteed to be visible by any other thread that reads a before reading text, as your example does:

  • the write of text happens-before the write to a in the writer thread
  • the write of a in writer happens-before the read of a in the main thread
  • the happens-before relation is transitive
  • hence the the write of text happens before the read of a.

这篇关于易失性变量和非易失性重新排序/可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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