volatile

C# compiler optimization and volatile keyword

荒凉一梦 提交于 2019-12-21 21:24:19
问题 I have read some posts about volatile keyword and behaviour without this keyword. I've especially tested the code from the answer to Illustrating usage of the volatile keyword in C#. When running, I observe the excepted behaviour in Release mode, without debugger attached. Up to that point, there is no problem. So, as far as I understand, the following code should never exit. public class Program { private bool stopThread; public void Test() { while (!stopThread) { } // Read stopThread which

The behaviour of making a non-volatile reference to a volatile object in Java

回眸只為那壹抹淺笑 提交于 2019-12-21 20:58:08
问题 Coming from C/C++, I am a little confused about volatile object behavior in Java. I understand that volatile in Java has two properties: Won't bring object into cache, always keep it in main memory. Guarantee "happen-before" However I am not sure what happens if I make a new non-volatile reference to object. For example, class Example { private volatile Book b = null; public init() { b = new Book(...); } public use() { Book local = b; local.read(); } } AFAIK, volatile means the "book object"

How does volatile keyword ensure an object`s fields are visible to other threads?

房东的猫 提交于 2019-12-21 20:36:31
问题 public class ObjectPropertiesVolatileTest { static Cup cup = new Cup(); public static void changeColor() { cup.setColor("black"); // change color of cup to black } public static void main(String[] args) { cup.setColor("red"); // initialize color of cup as red for(int i = 0; i < 3; i++) { new Thread(){ public void run() { for(int i = 0; i < 10; i++) System.out.println(Thread.currentThread().getName() + " " + i + " is " + cup.getColor()); if(Thread.currentThread().getName().equals("Thread-1"))

How to copy/set a volatile std::string?

那年仲夏 提交于 2019-12-21 20:34:18
问题 How can I copy a volatile std::string ? There is no copy constructor for volatile, nor does something like c_str allow volatile access. operator= also doesn't seem to allow setting a volatile. It seems like std::string is simply unusable as a volatile object. Is this intended, or am I missing some way to use it? NOTE: I have easy workarounds, I just came upon the issue while trying to use string in some low-level code. 回答1: As you noted, none of the member functions on std::string are marked

线程安全—可见性和有序性

馋奶兔 提交于 2019-12-21 18:57:04
在并发编程中,需要处理的两个关键问题: 线程之间如何通信 以及 线程之间如何同步 。 通信 是指线程之间以或者机制交换信息,java的并发采用的是共享内存模型,线程之间共享程序的公共状态,通过读写内存中的公共状态进行隐式通信。 同步 是是指程序中用于控制不同线程间操作发生相对顺序的机制。 最开始首先应该知道计算机中的 缓存 在其中起的作用 CPU Cache(高速缓存):由于计算机的存储设备与处理器的处理设备有着几个数量级的差距,所以现代计 算机都会加入一层读写速度与处理器处理速度接近相同的高级缓存来作为内存与处理器之间的缓冲,将运 算使用到的数据复制到缓存中,让运算能够快速的执行,当运算结束后,再从缓存同步到内存之中,这 样,CPU就不需要等待缓慢的内存读写了。 主(内)存:一个计算机包含一个主存,所有的CPU都可以访问主 存,主存比缓存容量大的多(CPU访问缓存层的速度快于访问主存的速度!但通常比访问内存寄存器的速度还是要慢点) 运作原理:通常情况下,当一个CPU要读取主存(RAM - Main Mernory)的时候,他会将主存中的数据读 取到CPU缓存中,甚至将缓存内容读到内部寄存器里面,然后再寄存器执行操作,当运行结束后,会 将寄存器中的值刷新回缓存中,并在某个时间点将值刷新回主存。 为什么需要CPU Cache? 答:CPU 的频率太快了,快到主存跟不上

thread safe without volatile

独自空忆成欢 提交于 2019-12-21 18:02:54
问题 Can anyone explain why this example is thread safe without volatile? http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html In fact, assuming that the computeHashCode function always returned the same result and had no side effects (i.e., idempotent), you could even get rid of all of the synchronization. // Lazy initialization 32-bit primitives // Thread-safe if computeHashCode is idempotent class Foo { private int cachedHashCode = 0; public int hashCode() { int h =

Java Concurrency : Volatile vs final in “cascaded” variables?

牧云@^-^@ 提交于 2019-12-21 13:09:24
问题 is final Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); the same as volatile Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); in case the

Does Java volatile prevent caching or enforce write-through caching?

让人想犯罪 __ 提交于 2019-12-21 08:24:27
问题 I'm trying to understand Java's volatile keyword with respect to writing to a volatile atomic variable in a multithreaded program with CPU caches. I've read several tutorials and the Java Language Specification, particularly section 17.4.5 on "happens-before ordering". My understanding is that when a thread writes a new value to a volatile variable, the updated value must be visible to other threads reading that variable. To me, these semantics can be implemented in one of two ways: Threads

Why does ostream prints `1` for a string defined as `volatile char[]`? [duplicate]

泄露秘密 提交于 2019-12-21 07:04:11
问题 This question already has answers here : Why does std::cout convert volatile pointers to bool? (4 answers) Closed 5 years ago . Consider this (artificial) example: #include <cstdio> #include <iostream> int main() { volatile char test[] = "abc"; std::printf("%s\n", test); std::cout << test << "\n"; } Compiling it with GCC and running gives the following output: $ g++ test.cc $ ./a.out abc 1 As you can see printf prints the string correctly while cout prints 1 . Why does writing to cout

.NET multithreading, volatile and memory model

落花浮王杯 提交于 2019-12-21 05:22:06
问题 Assume that we have the following code: class Program { static volatile bool flag1; static volatile bool flag2; static volatile int val; static void Main(string[] args) { for (int i = 0; i < 10000 * 10000; i++) { if (i % 500000 == 0) { Console.WriteLine("{0:#,0}",i); } flag1 = false; flag2 = false; val = 0; Parallel.Invoke(A1, A2); if (val == 0) throw new Exception(string.Format("{0:#,0}: {1}, {2}", i, flag1, flag2)); } } static void A1() { flag2 = true; if (flag1) val = 1; } static void A2()