volatile

Volatile keyword in microcontrollers [closed]

微笑、不失礼 提交于 2019-12-13 09:53:48
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . The volatile keyword tells the compiler not to optimize the variable which is prefixed. The variable may change during run time by unknown source (not known by the compiler) maybe by an external interrupt etc. Is there any other advantage of volatile ? Does volatile apply to

Java - volatile variable is not updating

十年热恋 提交于 2019-12-13 08:38:17
问题 I'm working on an interactive sorting application in JavaFx: The numbers are represented by rectangles Every time two numbers are swapped the rectangles are swapped(using timeline - animation) This is one of the sorting algorithms: public class BubbleSort implements SortAlgorithm { private volatile Boolean swaping; public void sort(double[] array, CompareFunction compareFunction, Model model, Controller controller) { Boolean ord; int i; double aux; swaping = false; do { ord = true; for (i = 0

Global variables modified by main() and accessed by ISR()

北城以北 提交于 2019-12-13 07:44:22
问题 Here is my c code char global_variable = 0; ISR(){ PORTA = global_variable; toggle_led;//to make sure that the interrupt is triggered } int main(){ while(1){ _delay_ms(500); gobal_variable++; PORTB = global_variable; } return 0; } The bottom line is that I have a global variable modified by the main function and read by both the main and ISR - interrupt handler . When the global variable is read by main I get the expected values, but in ISR I get the value that was first assigned to the

volatile variable being optimized away?

心已入冬 提交于 2019-12-13 06:45:46
问题 I have a background thread which loops on a state variable done . When I want to stop the thread I set the variable done to true . But apparently this variable is never set. I understand that the compiler might optimize it away so I have marked done volatile . But that seems not to have any effect. Note, I am not worried about race conditions so I have not made it atomic or used any synchronization constructs. How do I get the thread to not skip testing the variable at every iteration? Or is

Use of volatile in singleton double null check implementation in java

眉间皱痕 提交于 2019-12-13 06:28:44
问题 I want to know what is use of making Instance variable as volatile in double null check implementation of Singleton. Because as per my understanding synchronize block provide happens before implicitly. No two threads can access that synchronized block concurrently and while exiting from synchronized block, thread write all its local cached data to main memory. I search a lot but still i have doubts in this implementation. Please explain correct use. private volatile Singleton INSTANCE; public

Java基础 - volatile

我是研究僧i 提交于 2019-12-13 05:03:22
volatile的作用 :对与volatile修饰的变量, 1,保证该变量对所有线程的可见性。 2,禁止指令重排序。 Java内存模型(JMM) 原子性 i = 2; 把i加载到工作内存副本i,副本i=2,把 副本i 刷回主存。是原子的。 j = i; 从主存读 i和j 的副本到工作内存,把副本j=副本i,把副本j刷回主存。不是原子的。 i++; 即i=i+1;从主存读i的副本到工作内存,把副本i的值自增1,把副本i写回主存。不是原子的。 可见性 当一个变量被volatile修饰时,那么对它的修改会立刻刷新到主存,当其它线程需要读取该变量时(对于一条指令只读一次,比如i=i+1,读一次i;而while(flag) 每次判断只读一次,循环3次则读3次。这一点有助于理解volatile保证可见性却对非原子操作的指令i++无效),会去主存中读取最新值。 有序性 double pi = 3.14;//A double r = 1; //B double s= pi * r * r;//C 由于不会影响结果,JMM会对指令重排序,所以执行顺序可能是ABC,也可能是BAC,只要在单线程时不会影响结果。 假设某一指令有volatile变量,则该指令前的所有指令必然在该指令前执行,该指令后的所有指令必然在该指令后执行。 int a = 0; bool flag = false; public

Java reflection, add volatile modifier to private static field

不羁岁月 提交于 2019-12-13 03:17:54
问题 It's possible to add the volatile modifier to a field that is private and static? Example Code // I don't know when test is initalized public class Test { private static String secretString; public Test() { secretString = "random"; } } public class ReflectionTest extends Thread { public void run() { Class<?> testClass = Class.forName("Test"); Field testField = testClass.getDeclaredField("secretString"); while (testField.get(null) == null) { // Sleep, i don't know when test is initalized //

《Java并发编程实战》volatile变量

柔情痞子 提交于 2019-12-13 02:49:48
1.说明 当把变量声明为 volatile类型后,编译器与运行时都会注意到这个变量是共享的,因此不会将该变量上的操作与其他内存操作一起重排序。volatile修饰的变量不会被缓存在寄存器或对其他处理器不可见的地方,因此在读取volatile类型变量时总会返回最新写入的值,是一种比sychronized关键字更轻量级的同步机制。 读取volatile变量相当于进入同步代码块,而写入volatile变量相当于退出同步代码块。 2.使用场景 仅当volatile变量能简化代码的实现以及对同步策略的验证时,才应该使用;如果在验证正确性时需要对可见性进行复杂的判断,则不建议使用。 说人话就是: 1.对变量的写入操作不依赖变量的当前值; 2.该变量不会和其他状态变量一起纳入不变性条件中; 3.在访问该变量时不需要加锁。 e.g. 检查某个状态标记以判断是否退出循环。 3.与加锁的区别 加锁既保证了可见性,又保证了原子性。 volatile只保证了可见性。 如:volatile不能确保count++操作的原子性(count++包含了‘读-改-写’一组操作),故可以使用原子变量来实现,如 AtomicInteger等。 来源: CSDN 作者: 今辞拂衣 链接: https://blog.csdn.net/qq_21443203/article/details/103406938

const volatile pointer function argument

回眸只為那壹抹淺笑 提交于 2019-12-13 01:37:34
问题 For a embedded SW project we need to use some const volatile TYPE * pointers. Now we have some calculation functions which are looking like following: uint8 calc(const volatile uint8 *array, uint8 value) { ... } The data of both variables is not changing during the function execution. The calling code looks like following: const volatile uint8 *array = (const volatile uint8 *)0x00010111; uint8 value = 8; uint8 result = calc(array, value); The question is now, would be there a difference, if

Java多线程进阶—— J.U.C之atomic框架:AtomicInteger

风格不统一 提交于 2019-12-12 19:22:48
一、AtomicInteger简介 AtomicInteger,应该是atomic框架中用得最多的原子类了。顾名思义, AtomicInteger是Integer类型的线程安全原子类,可以在应用程序中以原子的方式更新int值 。 1. 创建AtomicInteger对象 先来看下AtomicInteger对象的创建。 AtomicInteger提供了两个构造器,使用默认构造器时,内部int类型的value值为0: AtomicInteger atomicInt = new AtomicInteger(); AtomicInteger类的内部并不复杂,所有的操作都针对内部的int值——value,并通过Unsafe类来实现线程安全的CAS操作: 2. AtomicInteger的使用 来看下面这个示例程序: public class Main { public static void main(String[] args) throws InterruptedException { AtomicInteger ai = new AtomicInteger(); List<Thread> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new Accumlator(ai),