volatile

double check locking without volatile (but with VarHandle release/acquire)

一个人想着一个人 提交于 2021-02-13 12:12:21
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

Volatile for structs and collections of structs

点点圈 提交于 2021-02-08 14:36:47
问题 I would like to use net wisdom to clarify some moments regarding multi-threading in .net. There are a lot of stuff in the internet about it however I was not able to find a good answer to my question. Let say we want to maintain a state of something in our class with safety for concurrent threads. Easy case is when state is int: class Class1 { volatile int state = 0; public int State { get { return state; } } public Action<int> StateUpdated; public void UpdateState(int newState) { state =

Volatile for structs and collections of structs

最后都变了- 提交于 2021-02-08 14:36:07
问题 I would like to use net wisdom to clarify some moments regarding multi-threading in .net. There are a lot of stuff in the internet about it however I was not able to find a good answer to my question. Let say we want to maintain a state of something in our class with safety for concurrent threads. Easy case is when state is int: class Class1 { volatile int state = 0; public int State { get { return state; } } public Action<int> StateUpdated; public void UpdateState(int newState) { state =

When would you need a volatile pointer?

徘徊边缘 提交于 2021-02-07 20:53:47
问题 I understand when you need a pointer to a volatile value (e.g. doing a read of a memory mapped peripheral). But when would you need the pointer itself to be volatile?> 回答1: To elaborate on my comment a little bit, it's important to first keep in mind what volatile means: it's a signal to the compiler that the variable being declared can change in unexpected ways through mechanisms that the compiler cannot see or control and that reads and writes may have side-effects. As a result, a compiler

When would you need a volatile pointer?

大城市里の小女人 提交于 2021-02-07 20:50:50
问题 I understand when you need a pointer to a volatile value (e.g. doing a read of a memory mapped peripheral). But when would you need the pointer itself to be volatile?> 回答1: To elaborate on my comment a little bit, it's important to first keep in mind what volatile means: it's a signal to the compiler that the variable being declared can change in unexpected ways through mechanisms that the compiler cannot see or control and that reads and writes may have side-effects. As a result, a compiler

How do signals interact with sequence points?

非 Y 不嫁゛ 提交于 2021-02-07 14:37:41
问题 The C89 standard states: At sequence points volatile objects are stable in the sense that previous evaluations are complete and subsequent evaluations have not yet occurred. The C89 standard also states: When the processing of the abstract machine is interrupted by receipt of a signal, only the values of objects as of the previous sequence point may be relied on. These requirements leave me confused, because I can't imagine how they would actually be implemented. I only have a rudimentary

How do signals interact with sequence points?

这一生的挚爱 提交于 2021-02-07 14:35:05
问题 The C89 standard states: At sequence points volatile objects are stable in the sense that previous evaluations are complete and subsequent evaluations have not yet occurred. The C89 standard also states: When the processing of the abstract machine is interrupted by receipt of a signal, only the values of objects as of the previous sequence point may be relied on. These requirements leave me confused, because I can't imagine how they would actually be implemented. I only have a rudimentary

Why is volatile keyword not allowed for local variables?

梦想与她 提交于 2021-02-07 06:51:17
问题 Consider the snippet: If in a main thread, I have this inside a method - volatile CountDownLatch latch = new CountDownLatch(3); new Thread(new ProcessThread("Worker1",latch, 20000)).start();//20 secs new Thread(new ProcessThread("Worker2",latch, 60000)).start();//60 secs new Thread(new ProcessThread("Worker3",latch, 40000)).start();//40 secs I see that volatile is shown as an illegal modifier. And only final is permitted. And final guarantees initialization safety . public static class

`volatile` to sync variable between threads

只愿长相守 提交于 2021-02-07 06:27:05
问题 I have a variable int foo that is accessed from two threads. Assuming I have no race-condition issues (access is protected by a mutex, all operations are atomic, or whatever other method to protect from race conditions), there is still the issue of "register caching" (for lack of a better name), where the compiler may assume that if the variable is read twice without being written in between, it is the same value, and so may "optimize" away things like: while(foo) { // <-may be optimized to

Can a pointer be volatile?

僤鯓⒐⒋嵵緔 提交于 2021-02-06 07:56:08
问题 Consider the following code: int square(volatile int *p) { return *p * *p; } Now, the volatile keyword indicates that the value in a memory location can be altered in ways unknown to the compiler or have other unknown side effects (e.g. modification via a signal interrupt, hardware register, or memory mapped I/O) even though nothing in the program code modifies the contents. So what exactly happens when we declare a pointer as volatile? Will the above mentioned code always work, or is it any