volatile

Safe to use volatile bool to force another thread to wait? (C++)

烂漫一生 提交于 2019-12-21 04:17:24
问题 Everything I've read about volatile says it's never safe, but I still feel inclined to try it, and I haven't seen this specific scenario declared unsafe. I have a separate thread that renders a scene, pulling data from the main simulation thread. This has no synchronization, and works fine. The issue is that when the program exits, then renderer needs to stop pulling data from the simulation thread before the simulation thread can safely clean itself up without causing the renderer to attempt

How do I know if gcc agrees that something is volatile?

霸气de小男生 提交于 2019-12-21 04:16:18
问题 Consider the following: volatile uint32_t i; How do I know if gcc did or did not treat i as volatile? It would be declared as such because no nearby code is going to modify it, and modification of it is likely due to some interrupt. I am not the world's worst assembly programmer, but I play one on TV. Can someone help me to understand how it would differ? If you take the following stupid code: #include <stdio.h> #include <inttypes.h> volatile uint32_t i; int main(void) { if (i == 64738)

If more than one thread can access a field should it be marked as volatile?

亡梦爱人 提交于 2019-12-21 04:16:14
问题 Reading a few threads (common concurrency problems, volatile keyword, memory model) I'm confused about concurrency issues in Java. I have a lot of fields that are accessed by more than one thread. Should I go through them and mark them all as volatile? When building a class I'm not aware of whether multiple threads will access it, so surely it is unsafe to let any field not be volatile, so by my understanding there's very few cases you wouldn't use it. Is this correct? For me this is specific

Volatile semantic with respect to other fields

丶灬走出姿态 提交于 2019-12-21 03:52:44
问题 Suppose I have following code private volatile Service service; public void setService(Service service) { this.service = service; } public void doWork() { service.doWork(); } Modified field marked as volatile and its value do not depend on previous state. So, this is correct multithreaded code (don't bother about Service implementations for a minute). As far as I know, reading volatile variable is like entering a lock, from perspective of memory visibility. It's because reading of normal

C# volatile array items?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 03:48:30
问题 I need an array with volatile items, and can't find a way to do that. private volatile T[] _arr; This means that the _arr reference is volatile, however it does not guarantee anything about the items inside the _arr object itself. Is there any way to mark the _arr's Items as volatile? Thanks. EDIT: The following code built according to binarycoder's answer. Is this code thread-safe to use? public class VolatileArray<T> { private T[] _arr; public VolatileArray(int length) { _arr = new T[length

volatile struct = struct not possible, why?

十年热恋 提交于 2019-12-21 03:14:23
问题 struct FOO{ int a; int b; int c; }; volatile struct FOO foo; int main(void) { foo.a = 10; foo.b = 10; foo.c = 10; struct FOO test = foo; return 0; } This won't compile, because struct FOO test = foo; generates an error: error: binding reference of type 'const FOO&' to 'volatile FOO' discards qualifiers How can I copy a volatile struct into another struct in C++ (before C++11)? Many people suggested to just delelte volatile, but I can't do that in that case, because I want to copy the current

long and double assignments are not atomic - How does it matter?

时光毁灭记忆、已成空白 提交于 2019-12-21 02:38:11
问题 We know that long and double assignments are not atomic in Java until they are declared volatile. My question is how does it really matter in our programming practice. for instance if you the see below classes whose objects are being shared among multiple threads. /** * The below class is not thread safe. the assignments to int values would be * atomic but at the same time it not guaranteed that changes would be visible to * other threads. **/ public final class SharedInt { private int value;

How do I declare an array created using malloc to be volatile in c++

只谈情不闲聊 提交于 2019-12-20 17:41:56
问题 I presume that the following will give me 10 volatile ints volatile int foo[10]; However, I don't think the following will do the same thing. volatile int* foo; foo = malloc(sizeof(int)*10); Please correct me if I am wrong about this and how I can have a volatile array of items using malloc. Thanks. 回答1: int volatile * foo; read from right to left "foo is a pointer to a volatile int" so whatever int you access through foo, the int will be volatile. P.S. int * volatile foo; // "foo is a

How can I use the volatile keyword in Java correctly?

∥☆過路亽.° 提交于 2019-12-20 12:08:09
问题 Say I have two threads and an object. One thread assigns the object: public void assign(MyObject o) { myObject = o; } Another thread uses the object: public void use() { myObject.use(); } Does the variable myObject have to be declared as volatile? I am trying to understand when to use volatile and when not, and this is puzzling me. Is it possible that the second thread keeps a reference to an old object in its local memory cache? If not, why not? Thanks a lot. 回答1: I am trying to understand

Is there any advantage of using volatile keyword in contrast to use the Interlocked class?

て烟熏妆下的殇ゞ 提交于 2019-12-20 10:44:44
问题 In other words, can I do something with a volatile variable that could not also be solved with a normal variable and the Interlocked class? 回答1: EDIT: question largely rewritten To answer this question, I dived a bit further in the matter and found out a few things about volatile and Interlocked that I wasn't aware of. Let's clear that out, not only for me, but for this discussion and other people reading up on this: volatile read/write are supposed to be immune to reordering. This only means