volatile

你真的了解JMM吗?

怎甘沉沦 提交于 2019-12-11 08:37:05
引言 在现代计算机中, cpu的指令速度远超内存的存取速度 ,由于计算机的存储设备与处理器的运算速度有几个数量级的差距,所以现代计算机系统都不得不 加入一层读写速度尽可能接近处理器运算速度的高速缓存(Cache) 来作为内存与处理器之间的缓冲: 将运算需要使用到的数据复制到缓存中,让运算能快速进行,当运算结束后再从缓存同步回内存之中,这样处理器就无须等待缓慢的内存读写了。 基于高速缓存的存储交互很好地解决了处理器与内存的速度矛盾,但是也为计算机系统带来更高的复杂度,因为它 引入了一个新的问题:缓存一致性(Cache Coherence) 。在多处理器系统中,每个处理器都有自己的高速缓存,而它们又共享同一主内存(MainMemory)。当多个处理器的运算任务都涉及同一块主内存区域时,将可能导致各自的缓存数据不一致,举例说明变量在多个CPU之间的共享。如果真的发生这种情况,那同步回到主内存时以谁的缓存数据为准呢?为了解决一致性的问题,需要各个处理器访问缓存时都遵循一些协议,在读写时要根据协议来进行操作,这类协议有MSI、MESI(Illinois Protocol)、MOSI、Synapse、Firefly及Dragon Protocol等。 一、JMM(Java Memory Model) java虚拟机规范定义 java内存模型屏蔽掉各种硬件和操作系统的内存访问差异

Is AtomicBoolean needed to create a cancellable thread?

半城伤御伤魂 提交于 2019-12-11 06:19:11
问题 I often use the following pattern to create a cancellable thread: public class CounterLoop implements Runnable { private volatile AtomicBoolean cancelPending = new AtomicBoolean(false); @Override public void run() { while (!cancelPending.get()) { //count } } public void cancel() { cancelPending.set(true); } } But I'm not sure that cancelPending MUST be a AtomicBoolean. Can we just use a normal boolean in this case? 回答1: Using both volatile and AtomicBoolean is unnecessary. If you declare the

Using volatile variables and semaphores - Java

。_饼干妹妹 提交于 2019-12-11 05:17:16
问题 I'm starting with Threads, Semaphores, volatile variables, etc. I wonder if when I'm using Semaphores it is necessary to define the variable as volatile, I mean: Having 2 Threads, one increases and the other decreases the variable for example and obviously, before each access I have a mutex that controls at any time only one thread is "playing" with the variable. It would be necessary to define as volatile? 回答1: From API doc of Semaphore: Memory consistency effects: Actions in a thread prior

With double-checked locking, does a put to a volatile ConcurrentHashMap have happens-before guarantee?

孤人 提交于 2019-12-11 04:24:03
问题 So far, I have used double-checked locking as follows: class Example { static Object o; volatile static boolean setupDone; private Example() { /* private constructor */ } getInstance() { if(!setupDone) { synchronized(Example.class) { if(/*still*/ !setupDone) { o = new String("typically a more complicated operation"); setupDone = true; } } } return o; } }// end of class Now, because we have groups of threads that all share this class, we changed the boolean to a ConcurrentHashMap as follows:

Can a reordering happen in the presence of a volatile variable? [duplicate]

浪尽此生 提交于 2019-12-11 03:38:41
问题 This question already has answers here : Happens-before relationships with volatile fields and synchronized blocks in Java - and their impact on non-volatile variables? (3 answers) Closed 5 years ago . I am doing as follows, there's only 2 threads in my program. // Thread 1 write a = 0 write a = 1 write volatile b = 1 // Thread 2 read volatile b // this I always do after write volatile b in the 1st thread read a I've read on Java Memory Model and from what I understand in thread 2 read a will

Why don't all member variables need volatile for thread safety even when using Monitor? (why does the model really work?)

我与影子孤独终老i 提交于 2019-12-11 02:19:49
问题 (I know they don't but I'm looking for the underlying reason this actually works without using volatile since there should be nothing preventing the compiler from storing a variable in a register without volatile... or is there...) This question stems from the discord of thought that without volatile the compiler (can optimize in theory any variable in various ways including storing it in a CPU register.) While the docs say that is not needed when using synchronization like lock around

Does armcc optimizes non-volatile variables with -O0?

浪子不回头ぞ 提交于 2019-12-11 00:35:47
问题 int* Register = 0x00FF0000; // Address of micro-seconds timer while(*Register != 0); Should I declare *Register as volatile while using armcc compiler and -O0 optimization ? In other words: Does -O0 optimization requires qualifying that sort of variables as volatile ? (which is probably required in -O2 optimization) 回答1: It seems to me that you should declare Register as volatile regardless, since it is volatile. There's no harm in marking it volatile , since you're depending on the compiler

C语言中volatile关键字的用法

谁都会走 提交于 2019-12-10 21:17:56
volatile是一个 类型修饰符 (type specifier),就像我们熟悉的const一样,它是被设计用来修饰被不同线程访问和修改的 变量 ;volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。 volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。 作用 编辑 简单地说就是防止编译器对代码进行优化。比如如下程序: 1 2 3 4 XBYTE[2]=0x55; XBYTE[2]=0x56; XBYTE[2]=0x57; XBYTE[2]=0x58; 对外部硬件而言,上述四条语句分别表示不同的操作,会产生四种不同的动作,但是编译器却会对上述四条语句进行优化,认为只有XBYTE[2]=0x58(即忽略前三条语句,只产生一条机器代码)。如果键入 volatile ,则编译器会逐一地进行编译并产生相应的机器代码(产生四条代码)。 例子 编辑 精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在 寄存器 里的备份。下面是volatile变量的几个例子: 1)并行设备的硬件寄存器(如:状态寄存器) 2)一个中断服务子程序中会访问到的非自动变量(Non-automatic variables) 3)多线程应用中被几个任务共享的变量 这是区分C程序员和 嵌入式系统

volatile array c++

偶尔善良 提交于 2019-12-10 21:15:25
问题 I have an application that has an array of pointers to MyObject objects: MyObject **arr; arr= new MyObject*[10]; The application has two threads, these threads will create and delete new MyObject() to array arr . Therefore arr[n] will be changed all the time, however the MyObject 's themselves do not change. Should I just declare: volatile MyObject **arr; Or should I go with: MyObject ** volatile arr; Thanks in advance 回答1: I think you need MyObject * volatile * arr; . Please note though that

Will volatile protect me from all behind-the-scenes multi-threading hazards?

懵懂的女人 提交于 2019-12-10 19:58:43
问题 I used to think there are only two multithreading hazards: Race condition : a thread reads x , and before it can write back another thread write x . Unstable state : a thread writes x and y , but between those two writes another thread reads x 's new value and y 's old value even though those variables should change together. I figured that if I can reason these things can't happen, I don't need to synchronize. But from an argument over the D language forums I learned there can be other