volatile

Is the popular “volatile polled flag” pattern broken?

拜拜、爱过 提交于 2019-12-20 09:56:00
问题 Suppose that I want to use a boolean status flag for cooperative cancellation between threads. (I realize that one should preferably use CancellationTokenSource instead; that is not the point of this question.) private volatile bool _stopping; public void Start() { var thread = new Thread(() => { while (!_stopping) { // Do computation lasting around 10 seconds. } }); thread.Start(); } public void Stop() { _stopping = true; } Question : If I call Start() at 0s and Stop() at 3s on another

How to cast away the volatile-ness?

社会主义新天地 提交于 2019-12-20 09:47:58
问题 How to cast away the volatile-ness? Which c++ style cast should I use? 回答1: Use const_cast . For example, volatile sample *pvs = new sample(); sample *ps = const_cast<sample*>(pvs); //casting away the volatile-ness That is, const_cast is used to cast away both const-ness as well as volatile-ness. Unfortunately, its name doesn't contain the term "volatile". Maybe, that is because the keyword const is more common in use than the keyword volatile . As one of the comment says, cv_cast would have

Java volatile and side-effects

烂漫一生 提交于 2019-12-20 09:46:09
问题 Oracle's documentation on atomic access (at http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html) says this: "a volatile variable establishes a happens-before relationship... . This means that ... when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change." I'm having trouble wrapping my head around that. I understand how volatile variables work (in >= Java 5), but I'm wondering

Can volatile but unfenced reads yield indefinitely stale values? (on real hardware)

蹲街弑〆低调 提交于 2019-12-20 09:29:53
问题 In answering this question a further question about the OP's situation came up that I was unsure about: it's mostly a processor architecture question, but with a knock-on question about the C++ 11 memory model as well. Basically, the OP's code was looping infinitely at higher optimization levels because of the following code (slightly modified for simplicity): while (true) { uint8_t ov = bits_; // bits_ is some "uint8_t" non-local variable if (ov & MASK) { continue; } if (ov == __sync_val

Are java variables themselves thread safe? When updating variables? [duplicate]

牧云@^-^@ 提交于 2019-12-20 06:44:09
问题 This question already has answers here : Thread-safe setting of a variable (Java)? (5 answers) Closed 3 years ago . Suppose I have two threads updating an object, and one thread reading from that object with no synchronization. Obviously, this is run condition. However, I am wondering if the variable itself can only partially written. public class CommonObject extends Object { static int memberVar=-1; } public class Input1Thread extends Thread { public void run() { while(true) CommonObject

Why doesn't volatile in Java update the value of a variable?

£可爱£侵袭症+ 提交于 2019-12-20 04:50:31
问题 I've read that "volatile" in Java allows different threads to have access to the same field and see changes the other threads has made to that field. If that's the case, I'd predict that when the first and second thread have completely run, the value of "d" will be incremented to 4. But instead, each thread increments "d" to a value of 2. public class VolatileExample extends Thread { private int countDown = 2; private volatile int d = 0; public VolatileExample(String name) { super(name);

关键字_Volatile

别说谁变了你拦得住时间么 提交于 2019-12-20 04:21:43
1.volatile 修饰符。   被设计 用来修饰 不同线程访问 和修改的 变量。如果不加volatile .基本上会导致:无法编写多线程程序,或者失去大量优化机会。 2.作用   作为指令的关键字,确保本条指令,不会因为编译器的优化而省略,要求每次直接读值。即防止编译器 对代码进行优化。   XBYTE[2]=0X55;   XBYTE[2]=0X56;   XBYTE[2]=0X57;   XBYTE[2]=0X58; 对外部硬件而言,四条语句,代表不同操作,产生四个不同动作。但是,编译器会对语句进行优化,认委只有 XBYTE[2]=0X58;而忽略前三条,如果加上volatile ,则逐条编译。 3.举例。   优化器,在每次用到这个变量时,必须每次重新读取这个变量的值,而不是读取 寄存器中的 备份。 int squre(volatile int* &ptr){   int a,b;   a=*ptr;   b=*ptr;   return a*b; } 结果,可能并不是 所期待的平方之,因为 执行过程中,两次取值可能会发生改变。 关键在于两个地方: (1)编译器的优化   本次线程内,当读取一个变量,为了提高存取速度,编译器 优化时,会把一个变量读取到寄存器中。以后再取变量,就直接从寄存器读取。当变量值在本线程发生改变,会把变量的新值,从新copy到寄存器/。保持一致。

Volatile关键字

耗尽温柔 提交于 2019-12-20 04:10:01
1.什么是Volatile ThreadLocal可见性也就是说一旦某个线程修改了该被volatile修饰的变量,它会保证修改的值会立即被更新到主存,当有其他线程需要读取时,可以立即获取修改之后的值。 ThreadLocal在Java中为了加快程序的运行效率,对一些变量的操作通常是在该线程的寄存器或是CPU缓存上进行的,之后才会同步到主存中,而加了volatile修饰符的变量则是直接读写主存。 ThreadLocalVolatile 保证了线程间共享变量的及时可见性,但不能保证原子性 2.JMM(Java内存模型) 要搞懂Volatile首先要了解一下Java内存模型 共享内存模型指的就是Java内存模型(简称JMM), JMM决定一个线程对共享变量的写入时,能对另一个线程可见 。从抽象的角度来看,JMM定义了线程和主内存之间的抽象关系: 线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地内存(local memory),本地内存中存储了该线程以读/写共享变量的副本 。本地内存是JMM的一个抽象概念,并不真实存在。它涵盖了缓存,写缓冲区,寄存器以及其他的硬件和编译器优化。 从上图来看,线程A与线程B之间如要通信的话,必须要经历下面2个步骤: (1) 首先,线程A把本地内存A中更新过的共享变量刷新到主内存中去。 (2) 然后

JAVA CAS原理深度分析

点点圈 提交于 2019-12-20 02:59:47
CAS CAS:Compare and Swap, 翻译成比较并交换。 java.util.concurrent包中借助CAS实现了区别于synchronouse同步锁的一种乐观锁。 本文先从CAS的应用说起,再深入原理解析。 CAS应用 CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B。当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做。 非阻塞算法 (nonblocking algorithms) 一个线程的失败或者挂起不应该影响其他线程的失败或挂起的算法。 现代的CPU提供了特殊的指令,可以自动更新共享数据,而且能够检测到其他线程的干扰,而 compareAndSet() 就用这些代替了锁定。 拿出AtomicInteger来研究在没有锁的情况下是如何做到数据正确性的。 private volatile int value; 首先毫无以为,在没有锁的机制下可能需要借助volatile原语,保证线程间的数据是可见的(共享的)。 这样才获取变量的值的时候才能直接读取。 public final int get() { return value; } 然后来看看++i是怎么做到的。 public final int incrementAndGet() { for (;;) { int current = get(); int next = current

JAVA CAS原理深度分析

泪湿孤枕 提交于 2019-12-20 02:59:07
参考文档: http://www.blogjava.net/xylz/archive/2010/07/04/325206.html http://blog.hesey.net/2011/09/resolve-aba-by-atomicstampedreference.html http://www.searchsoa.com.cn/showcontent_69238.htm http://ifeve.com/atomic-operation/ http://www.infoq.com/cn/articles/java-memory-model-5 java.util.concurrent包完全建立在CAS之上的,没有CAS就不会有此包。可见CAS的重要性。 CAS CAS:Compare and Swap, 翻译成比较并交换。 java.util.concurrent包中借助CAS实现了区别于synchronouse同步锁的一种乐观锁。 本文先从CAS的应用说起,再深入原理解析。 CAS应用 CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B。当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做。 非阻塞算法 (nonblocking algorithms) 一个线程的失败或者挂起不应该影响其他线程的失败或挂起的算法。 现代的CPU提供了特殊的指令