volatile

Volatile: why prevent compiler reorder code

不问归期 提交于 2019-11-30 11:52:49
问题 In java, with my knowledge, volatile variable make a thread reads/writes directly to main CPU (not in cache of each thread), so make its change visibles to other threads. The thing I don't know is : So, why this work (of volatile) can prevent compiler/CPU reorder statement of code. thanks :) 回答1: Here is a very good example illustrating the issue the prohibition on reordering is aimed to address (taken from here): class VolatileExample { int x = 0; volatile boolean v = false; public void

CancellationTokenSource vs. volatile boolean

故事扮演 提交于 2019-11-30 11:18:24
Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish? Of course yes. There are many. I'll list few. CancellationToken supports callbacks. You can be notified when the cancellation is requested. CancellationToken supports WaitHandle which you could wait for indefinitely or with a timeout. You can schedule the cancelation of CancellationToken using CancellationTokenSource.CancelAfter method. You can link your CancellationToken to another, so that when one is cancelled another can be considered as cancelled. By Task if you mean

Should std::atomic be volatile?

安稳与你 提交于 2019-11-30 11:09:34
I'm running a thread that runs until a flag is set. std::atomic<bool> stop(false); void f() { while(!stop.load(std::memory_order_{relaxed,acquire})) { do_the_job(); } } I wonder if the compiler can unroll loop like this (I don't want it to happen). void f() { while(!stop.load(std::memory_order_{relaxed,acquire})) { do_the_job(); do_the_job(); do_the_job(); do_the_job(); ... // unroll as many as the compiler wants } } It is said that volatility and atomicity are orthogonal, but I'm a bit confused. Is the compiler free to cache the value of the atomic variable and unroll the loop? If the

Usage wise difference between const & volatile qualifier in C?

别来无恙 提交于 2019-11-30 10:51:24
I have already gone through the answer of question @ What are the differences between const and volatile pointer in C? I understand the explanation that: The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. However, volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data. Which implies that both type of variables can be changed by external event. But,then where is the difference in usage of const & volatile? In C,

If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

安稳与你 提交于 2019-11-30 10:13:14
I've looked at this answer , and it states how: Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are guaranteed now to be visible to B. Therefore, given the example: public class Main { static int value = -1; static volatile boolean read; public static void main(String[] args) { Thread a = new Thread(() -> { value = 1; read = true; }); Thread b = new Thread(() -> { while (!read); System.out.println("Value: " + value); }); a.start(); b.start(); } } Is the change to

Where to use volatile? [duplicate]

社会主义新天地 提交于 2019-11-30 09:46:12
This question already has an answer here: Why is volatile needed in C? 17 answers I read about volatile keyword, but I don't know in what situations I should use it. When the memory (variable) is getting updated and process is not aware of that? In what cases should drivers use volatile variables? The most common case in my world is when you are programming microcontrollers that use memory-mapped I/O. The value in a register could change due to external digital inputs, but if you don't declare a variable as volatile , the compiler might optimize the code out completely and you'll be wondering

JAVA并发容器-ConcurrentLinkedQueue 源码分析

元气小坏坏 提交于 2019-11-30 09:36:43
在并发编程中,有时候需要使用线程安全的队列。如果要实现一个线程安全的队列有两 种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁 (入队和出队用同一把锁)或两个锁(入队和出队用不同的锁)等方式来实现。非阻塞的实现方式则可以使用循环CAS的方式来实现。 ConcurrentLinkedQueue是一个 非阻塞的基于链表节点的无界线程安全队列 ,它遵循先进先出的原则,并采用了“wait-free”算法(即CAS算法)来实现。 类关系图 核心属性 // 头结点指针(不一定指向头,缓更新,非阻塞的,所以没法保证) private volatile Node<E> head; // 尾节点指针(不一定指向尾,缓更新,非阻塞的,所以没法保证) private volatile Node<E> tail; // 链表节点 private static class Node<E> { // 节点元素(存数据) volatile E item; // 下一个节点 volatile Node<E> next; ... } ConcurrentLinkedQueue由head节点和tail节点组成,每个节点(Node)由节点元素(item)和 指向下一个节点(next)的引用组成,节点与节点之间就是通过这个next关联起来,从而组成一 张链表结构的队列

c# volatile的用法

青春壹個敷衍的年華 提交于 2019-11-30 09:32:52
注:本文为个人学习摘录,原文地址:http://www.cnblogs.com/gjhjoy/p/3556709.html 恐怕比较一下volatile和synchronized的不同是最容易解释清楚的。volatile是变量修饰符,而synchronized则作用于一段代码或方法;看如下三句get代码: int i1; int geti1() {return i1;} volatile int i2; int geti2() {return i2;} int i3; synchronized int geti3() {return i3;}   geti1()得到存储在当前线程中i1的数值。多个线程有多个i1变量拷贝,而且这些i1之间可以互不相同。换句话说,另一个线程可能已经改 变了它线程内的i1值,而这个值可以和当前线程中的i1值不相同。事实上,Java有个思想叫“主”内存区域,这里存放了变量目前的“准确值”。每个线程 可以有它自己的变量拷贝,而这个变量拷贝值可以和“主”内存区域里存放的不同。因此实际上存在一种可能:“主”内存区域里的i1值是1,线程1里的i1值 是2,线程2里的i1值是3——这在线程1和线程2都改变了它们各自的i1值,而且这个改变还没来得及传递给“主”内存区域或其他线程时就会发生。    而geti2()得到的是“主”内存区域的i2数值

java设计模式——单例模式

隐身守侯 提交于 2019-11-30 09:21:53
单例模式的使用动机 某些系统中的某些类的对象只需要一个, 或者只能是一个, 如果多于一个甚至会出现错误,这时候我们就要用到单例模式。日志对象, 打印池对象, 序列ID生成器等应用。单例模式可以让系统在减少内存空间的情况下仍然能正常工作。 生成单例的方式:1.懒汉式(线程不安全);2.懒汉式(线程安全);3.饿汉式;4.静态内部类; 5.双重检验锁;6.枚举 这里只介绍下:双重检验锁。貌似现在都这个比较多。 其他单例模式可以参考: https://www.zybuluo.com/pastqing/note/164787 http://cantellow.iteye.com/blog/838473 双重校验锁 public class Singleton { private volatile static Singleton singleton; private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } } 为什么要判断两次 singleton

Should ALL global variables be volatile-qualified?

北城以北 提交于 2019-11-30 09:10:01
In this example, does correctness require global_value to be declared volatile ? int global_value = 0; void foo () { ++ global_value; } void bar () { some_function (++global_value); foo (); some_function (++global_value); } My understanding is that volatile is "intended" for pointers to mapped memory and variables which can be modified by signals (and emphatically not for thread-safety) but it's easy to imagine that bar might compile to something like this: push EAX mov EAX, global_value inc EAX push EAX call some_function call foo inc EAX push EAX call some_function mov global_value, EAX pop