volatile

How does volatile work with const?

一个人想着一个人 提交于 2020-07-04 04:27:54
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

How does volatile work with const?

夙愿已清 提交于 2020-07-04 04:27:11
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

How does volatile work with const?

…衆ロ難τιáo~ 提交于 2020-07-04 04:26:55
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

Simulating Field-visibility problem in Java

柔情痞子 提交于 2020-06-26 06:54:37
问题 I was going through one of the tutorials on memory model of Java and came across this concept of field visibility which happens in multi-threaded programming. I tried to simulate the same using the below code, however , I see in each thread, the latest value is being reflected (in ReaderThread). The below is the complete program. Edit After some suggestion to use while(somevariable), I incorporated, but still getting the same behaviour. I removed sysout on reading the x FieldVisibility.java

Simulating Field-visibility problem in Java

爷,独闯天下 提交于 2020-06-26 06:54:29
问题 I was going through one of the tutorials on memory model of Java and came across this concept of field visibility which happens in multi-threaded programming. I tried to simulate the same using the below code, however , I see in each thread, the latest value is being reflected (in ReaderThread). The below is the complete program. Edit After some suggestion to use while(somevariable), I incorporated, but still getting the same behaviour. I removed sysout on reading the x FieldVisibility.java

Simulating Field-visibility problem in Java

老子叫甜甜 提交于 2020-06-26 06:54:00
问题 I was going through one of the tutorials on memory model of Java and came across this concept of field visibility which happens in multi-threaded programming. I tried to simulate the same using the below code, however , I see in each thread, the latest value is being reflected (in ReaderThread). The below is the complete program. Edit After some suggestion to use while(somevariable), I incorporated, but still getting the same behaviour. I removed sysout on reading the x FieldVisibility.java

Declaring an object as volatile

假装没事ソ 提交于 2020-05-24 22:40:30
问题 If you declare a member variable as volatile in Java, does this mean that all the object's data is stored in volatile memory, or that the reference to the object is stored in volatile memory? For example if I have the following class: class C{ int i = 0; char c = 'c'; } If I declare an instance of it as follows: private volatile C obj; does that store the reference to obj in volatile memory, or obj 's data ( obj.i and obj.c ) in volatile memory? Does it make obj.c and obj.i thread safe or not

Volatile member variables vs. volatile object?

亡梦爱人 提交于 2020-05-14 03:52:22
问题 I'm trying to implement a multiple producer (via interrupt), single consumer (via application thread) queue on an embedded target in "MpscQueue.h" below. I'm wondering if I can safely remove the some of the volatile usage below (see inline questions). I would also consider using volatile std::array in place of the C-style buffer_[] shown below, but I was unsure if I could trust its implementation to match the intent of the below. A third alternative would be to mark the MpscQueue object

Volatile member variables vs. volatile object?

拟墨画扇 提交于 2020-05-14 03:51:27
问题 I'm trying to implement a multiple producer (via interrupt), single consumer (via application thread) queue on an embedded target in "MpscQueue.h" below. I'm wondering if I can safely remove the some of the volatile usage below (see inline questions). I would also consider using volatile std::array in place of the C-style buffer_[] shown below, but I was unsure if I could trust its implementation to match the intent of the below. A third alternative would be to mark the MpscQueue object

do we need volatile when implementing singleton using double-check locking

懵懂的女人 提交于 2020-05-09 06:05:32
问题 suppose we use double-check lock to implement singleton pattern: private static Singleton instance; private static Object lock = new Object(); public static Singleton getInstance() { if(instance == null) { synchronized (lock) { if(instance == null) { instance = new Singleton(); } } } return instance; } Do we need to set variable "instance" as "volatile"? I hear a saying that we need it to disable reordering: When an object is created , reordering may happen: address=alloc instance=someAddress