volatile

Should ALL global variables be volatile-qualified?

自古美人都是妖i 提交于 2019-12-18 13:14:18
问题 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

How to force an unused memory read in C that won't be optimized away?

a 夏天 提交于 2019-12-18 12:52:28
问题 Microcontrollers often require a register to be read to clear certain status conditions. Is there a portable way in C to ensure that a read is not optimized away if the data is not used? Is it sufficient that the pointer to the memory mapped register is declared as volatile? In other words, would the following always work on standard compliant compilers? void func(void) { volatile unsigned int *REGISTER = (volatile unsigned int *) 0x12345678; *REGISTER; } I understand that dealing with

How to force an unused memory read in C that won't be optimized away?

烈酒焚心 提交于 2019-12-18 12:52:28
问题 Microcontrollers often require a register to be read to clear certain status conditions. Is there a portable way in C to ensure that a read is not optimized away if the data is not used? Is it sufficient that the pointer to the memory mapped register is declared as volatile? In other words, would the following always work on standard compliant compilers? void func(void) { volatile unsigned int *REGISTER = (volatile unsigned int *) 0x12345678; *REGISTER; } I understand that dealing with

How deep volatile publication guarantees?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 11:13:18
问题 As is known guarant that if we have some object reference and this reference has final field - we will see all reachable fields from final field(at least when constructor was finished) example 1: class Foo{ private final Map map; Foo(){ map = new HashMap(); map.put(1,"object"); } public void bar(){ System.out.println(map.get(1)); } } As I undertand at this case we have guarantee that bar() method always output object because: 1. I listed full code of class Foo and map is final; 2. If some

How deep volatile publication guarantees?

微笑、不失礼 提交于 2019-12-18 11:13:15
问题 As is known guarant that if we have some object reference and this reference has final field - we will see all reachable fields from final field(at least when constructor was finished) example 1: class Foo{ private final Map map; Foo(){ map = new HashMap(); map.put(1,"object"); } public void bar(){ System.out.println(map.get(1)); } } As I undertand at this case we have guarantee that bar() method always output object because: 1. I listed full code of class Foo and map is final; 2. If some

单例模式的优缺点

蹲街弑〆低调 提交于 2019-12-18 09:50:08
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、时间和空间 比较上面两种写法:懒汉式是典型的时间换空间,也就是每次获取实例都会进行判断,看是否需要创建实例,浪费判断的时间。当然,如果一直没有人使用的话,那就不会创建实例,则节约内存空间。 饿汉式是典型的空间换时间,当类装载的时候就会创建类实例,不管你用不用,先创建出来,然后每次调用的时候,就不需要再判断了,节省了运行时间。 2、线程安全 (1)从线程安全性上讲,不加同步的懒汉式是线程不安全的,比如,有两个线程,一个是线程A,一个是线程B,它们同时调用getInstance方法,那就可能导致并发问题。如下示例: 复制代码 public static Singleton getInstance(){ if(instance == null){ instance = new Singleton(); } return instance; } 复制代码 程序继续运行,两个线程都向前走了一步,如下: 复制代码 public static Singleton getInstance(){ if(instance == null){ instance = new Singleton(); } return instance; } 复制代码 (2)饿汉式是线程安全的,因为虚拟机保证只会装载一次

unused volatile variable

依然范特西╮ 提交于 2019-12-18 09:11:23
问题 If i declare a variable as volatile and if I dont use it any where in the program, will the compiler optimize that variable ? What in case of local and global declarations of volatile variables in this case? tq. 回答1: The compiler can, and probably will, eliminate (ignore) the unused volatile variable declaration (but the compiler cannot eliminate an unused global variable definition - it has to assume that some other translation unit (TU) will reference it). If the variable is local to the

Do memory barriers guarantee a fresh read in C#?

淺唱寂寞╮ 提交于 2019-12-18 06:56:42
问题 If we have the following code in C#: int a = 0; int b = 0; void A() // runs in thread A { a = 1; Thread.MemoryBarrier(); Console.WriteLine(b); } void B() // runs in thread B { b = 1; Thread.MemoryBarrier(); Console.WriteLine(a); } The MemoryBarriers make sure that the write instruction takes place before the read. However, is it guaranteed that the write of one thread is seen by the read on the other thread? In other words, is it guaranteed that at least one thread prints 1 or both thread

Modifying a const variable with the volatile keyword

回眸只為那壹抹淺笑 提交于 2019-12-18 06:51:56
问题 I was answering a question and made this test program. #include <stdio.h> int main() { volatile const int v = 5; int * a = &v; *a =4; printf("%d\n", v); return 0; } Without the volatile keyword the code optimizes (compiled with -O3 apple clang 4.2) the change of the var away, with it works as expected and the const variable is modified correctly. I was wondering if a more experienced C developer knows if there is a part of the standard that says this is unsafe or UB. UPDATE: @EricPostpischil

Modifying a const variable with the volatile keyword

ぃ、小莉子 提交于 2019-12-18 06:50:24
问题 I was answering a question and made this test program. #include <stdio.h> int main() { volatile const int v = 5; int * a = &v; *a =4; printf("%d\n", v); return 0; } Without the volatile keyword the code optimizes (compiled with -O3 apple clang 4.2) the change of the var away, with it works as expected and the const variable is modified correctly. I was wondering if a more experienced C developer knows if there is a part of the standard that says this is unsafe or UB. UPDATE: @EricPostpischil