在易失性场周围重新排列法线场 [英] Reorder normal field around volatile field

查看:52
本文介绍了在易失性场周围重新排列法线场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于

volatile有什么作用? http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#incorrectlySync

易失性的新保证 http://www. ibm.com/developerworks/library/j-jtp03304/

class VolatileExample {
  int x = 0;
  volatile boolean v = false;
  public void writer() {
    x = 42;
    v = true;
  }

  public void reader() {
    if (v == true) {
      //uses x - guaranteed to see 42.
    }
  }
}

看来.

1a) write to non-volatile variable x 
1b) write to volatile variable v

1a永远不会移动通行证1b

我想知道是否将源代码修改为以下内容

class VolatileExample {
  int x = 42;
  volatile boolean v = true;
  public void writer() {
    v = false;
    x = 0;
  }

  public void reader() {
    if (v == true) {
      //uses x - guaranteed to see 42?????
    }
  }
}

下面的序列可以排列吗?

2a) write to volatile variable v
2b) write to non-volatile variable x

我想知道2b能否在2a之前移动?这是因为如果2b能够在2a之前移动,那么读者将无法再保证在if块内看到42.

根据以下信息,我认为2b可以在2a之前移动.


http://www.cs .umd.edu/〜pugh/java/memoryModel/jsr-133-faq.html#reordering

写入易失性字段具有 与监视器具有相同的记忆效果 释放,并从易失性中读取 栏位的记忆效果与 监控获取.


这意味着任何内存操作 之前对线程可见的 退出同步块 进入后对任何线程可见 同步块受 同一台显示器,因为所有内存 操作发生在发布之前, 而发布发生在 获取.


蟑螂汽车旅馆和Java内存模型

volatile_v = true;      <-- monitor release
non_volatile_x = 42;
(volatile_v will act as a roach motels, and it is fine for non_volatile_x to move into roach motels)


non_volatile_x = 42;
volatile_v = true;      <-- monitor release
(volatile_v will act as a roach motels, and it is not OK for non_volatile_x to move out from roach motels)

解决方案

我们知道,对volatile变量的写入不能相对于以前的任何读取或写入(从Java5开始)进行重新排序,但这不是反之亦然.因此,将程序重新排序为x = 0; v =假;据我所知是正确的.

写入v后,可以保证读取v时可以看到写入v之前发生的每个动作,但是它并没有说明写入v之后的动作-这些可能发生或未发生甚至可以重新排序以将其写入v.

Based on

What does volatile do? http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#incorrectlySync

and

New guarantees for volatile http://www.ibm.com/developerworks/library/j-jtp03304/

class VolatileExample {
  int x = 0;
  volatile boolean v = false;
  public void writer() {
    x = 42;
    v = true;
  }

  public void reader() {
    if (v == true) {
      //uses x - guaranteed to see 42.
    }
  }
}

It seems that.

1a) write to non-volatile variable x 
1b) write to volatile variable v

1a can never moved pass 1b

I was wondering, if I modify the source code to the following

class VolatileExample {
  int x = 42;
  volatile boolean v = true;
  public void writer() {
    v = false;
    x = 0;
  }

  public void reader() {
    if (v == true) {
      //uses x - guaranteed to see 42?????
    }
  }
}

Can the following sequence be permuted?

2a) write to volatile variable v
2b) write to non-volatile variable x

I was wondering, can 2b ever move before 2a? This is because if 2b able to move before 2a, reader can no longer guaranteed to see 42 within if block.

I feel 2b can be move before 2a based on the following information.


http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#reordering

Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire.


This means that any memory operations which were visible to a thread before exiting a synchronized block are visible to any thread after it enters a synchronized block protected by the same monitor, since all the memory operations happen before the release, and the release happens before the acquire.


and Roach Motels and The Java Memory Model

volatile_v = true;      <-- monitor release
non_volatile_x = 42;
(volatile_v will act as a roach motels, and it is fine for non_volatile_x to move into roach motels)


non_volatile_x = 42;
volatile_v = true;      <-- monitor release
(volatile_v will act as a roach motels, and it is not OK for non_volatile_x to move out from roach motels)

解决方案

Well we know that a write to a volatile variable can't be reordered with respect to any previous read or write (as of Java5), but this isn't true the other way around. So reordering the program to x = 0; v = false; would be correct as I understand it.

After we write to v we're guaranteed that when reading v that every action that happened before writing to v will be visible, but it doesn't say anything about actions AFTER writing to v - those may or may not have happened and can even be reordered to happen before v is written to.

这篇关于在易失性场周围重新排列法线场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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