volatile

volatile

戏子无情 提交于 2019-12-03 20:13:26
# 关于volatile的相关知识,主要阅读了一些文章 文章1 文章2 文章3 # 但是阅读了这些文章之后,总想着自己写写代码验证一下 public class QQ { private static String name = "init"; private static boolean flag = false; public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { System.out.println("thread1 start"); name = "yukong"; flag = true; System.out.println("thread1 end"); }); Thread thread2 = new Thread(() -> { while (true) { if (flag) { System.out.println("flag = " + flag + ", name=" + name); break; } } }); thread2.start(); Thread.sleep(1000); thread1.start(); } }   - 上面代码的执行结果是thread1执行完成,thread2无输出

Memory Model: preventing store-release and load-acquire reordering

本小妞迷上赌 提交于 2019-12-03 19:48:07
问题 It is known that, unlike Java's volatiles, .NET's ones allow reordering of volatile writes with the following volatile reads from another location. When it is a problem MemoryBarier is recommended to be placed between them, or Interlocked.Exchange can be used instead of volatile write. It works but MemoryBarier could be a performance killer when used in highly optimized lock-free code. I thought about it a bit and came with an idea. I want somebody to tell me if I took the right way. So, the

Volatile Variables and Happens before ordering [duplicate]

冷暖自知 提交于 2019-12-03 18:51:47
问题 This question already has answers here : Java memory model: volatile variables and happens-before (3 answers) Closed 4 years ago . I have two threads: Thread:1 a = 1; x = b; Thread:2 b = 1 y = a Here a and b are declared volatile. I did not understand how a "happens-before" edge is created between a = 1; and y = a; and between x = b; and b = 1; I understand that by using volatile variable one can prevent reading stale values from thread cache. But how can a volatile variable ensure happens

Java: volatile enough to make classes threadsafe?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 18:04:21
I have a question about the volatile statement in Java. Please look at this constructed example: class Master { // Foo is a class with thread-safe methods public volatile Foo foo; } class Worker1 implements Runnable { protected Master master void run() { ... }; } class Worker2 implements Runnable { protected Master master void run() { ... }; } We have 2 worker threads which hold a reference to an object of class Master running at the same time. During their work, both have to access methods of master.foo. At some point, the Foo object of master will be changed by one of the worker threads. Now

when should a member function be both const and volatile together?

断了今生、忘了曾经 提交于 2019-12-03 17:19:57
问题 I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together . I didn't get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together. I wrote small class to test the same: class Temp { public: Temp(int x) : X(x) { } int getX() const volatile { return X; } int getBiggerX() { return X + 10; } private: int X; }; void test( const

Java内存模型相关原则详解

假如想象 提交于 2019-12-03 17:13:30
在《 Java内存模型(JMM)详解 》一文中我们已经讲到了Java内存模型的基本结构以及相关操作和规则。而Java内存模型又是围绕着在并发过程中如何处理原子性、可见性以及有序性这三个特征来构建的。本篇文章就带大家了解一下相关概念、原则等内容。 原子性 原子性即一个操作或一系列是不可中断的。即使是在多个线程的情况下,操作一旦开始,就不会被其他线程干扰。 比如,对于一个静态变量int x两条线程同时对其赋值,线程A赋值为1,而线程B赋值为2,不管线程如何运行,最终x的值要么是1,要么是2,线程A和线程B间的操作是没有干扰的,这就是原子性操作,不可被中断的。 Java内存模型对以下操作保证其原子性:read,load,assign,use,store,write。我们可以大致认为基本数据类型的访问读写是具备原子性的(前面也提到了long和double类型的“半个变量”情况,不过几乎不会发生)。 从Java内存模型底层来看有上面的原子性操作,但针对用户来说,也就是我们编写Java的程序,如果需要更大范围的原子性保障,就需要同步关键字——synchronized来保障了。也就是说synchronized中的操作也具有原子性。 可见性 可见性是指当一个线程修改了共享变量的值,其他线程能够立即得知这个修改。 Java内存模型是通过变量修改后将新值同步回主内存,在变量读取前从主内存刷新变量值

Why is (or isn't) setting fields in a constructor thread-safe?

爱⌒轻易说出口 提交于 2019-12-03 16:29:34
问题 Let's say you have a simple class like this: class MyClass { private readonly int a; private int b; public MyClass(int a, int b) { this.a = a; this.b = b; } public int A { get { return a; } } public int B { get { return b; } } } I could use this class in a multi-threaded manner: MyClass value = null; Task.Run(() => { while (true) { value = new MyClass(1, 1); Thread.Sleep(10); } }); while (true) { MyClass result = value; if (result != null && (result.A != 1 || result.B != 1)) { throw new

Non-volatile UDF always recalculating

爱⌒轻易说出口 提交于 2019-12-03 14:25:41
问题 I am trying to make a non-volatile UDF but it seems not possible. So here is a my very simple test-UDF: Option Explicit Dim i As Integer Sub Main() i = 0 [A1] = "zyy" MsgBox i End Sub Function Test(rng As Range) Application.Volatile (False) Test = rng.Value i = i + 1 End Function I got a otherwise empty worksheet that uses this function a couple of times, and every time I call Main() and change any cell on the sheet with the UDFs all of them recalculate. How can I make this (any) UDF no

Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

怎甘沉沦 提交于 2019-12-03 14:21:49
I've been reading the answer to a similar question , but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about: ...declaring a variable volatile makes it volatile for every single access. It is impossible to force this behavior any other way, hence volatile cannot be replaced with Interlocked. This is needed in scenarios where other libraries, interfaces or hardware can access your variable and update it anytime, or need the most recent version. Does Interlocked guarantee visibility of the atomic operation to all threads, or do I still have to use

Volatile fields: How can I actually get the latest written value to a field?

家住魔仙堡 提交于 2019-12-03 14:01:20
Considering the following example: private int sharedState = 0; private void FirstThread() { Volatile.Write(ref sharedState, 1); } private void SecondThread() { int sharedStateSnapshot = Volatile.Read(ref sharedState); Console.WriteLine(sharedStateSnapshot); } Until recently, I was under the impression that, as long as FirstThread() really did execute before SecondThread() , this program could not output anything but 1 . However, my understanding now is that: Volatile.Write() emits a release fence. This means no preceding load or store (in program order) may happen after the assignment of 1 to