volatile

C# bool is atomic, why is volatile valid

爷,独闯天下 提交于 2019-12-04 17:43:33
问题 In C# , we know that a bool is atomic - then why is it valid to mark it as volatile ? what is the difference and what is a good (or even practical) use-case for one versus the other? bool _isPending; Versus volatile bool _isPending; // Is this realistic, or insanity? I have done some reading here and here, and I'm trying to ensure that I fully understand the inner workings of the two. I want to understand when it is appropriate to use one vs the other, or if just bool is enough. 回答1: In C#,

多线程之美1一volatile

社会主义新天地 提交于 2019-12-04 17:38:50
目录 一、java内存模型 1.1、抽象结构图 1.2、概念介绍 二、volatile详解 2.1、概念 2.2、保证内存可见性 2.3、不保证原子性 2.4、有序性 一、java内存模型 1.1、抽象结构图 1.2、概念介绍 java 内存模型 即Java memory model(简称JMM), java线程之间的通信由JMM控制,决定一个线程对共享变量的写入何时对另一个线程可见。 多线程通信通常分为2类:共享内存和消息传递 JMM采用的就是共享内存来实现线程间的通信,且通信是隐式的,对程序开发人员是透明的,所以在了解其原理了,才会对线程之间通信,同步,内存可见性问题有进一步认识,避免开发中出错。 线程之间如何通信? 在java中多个线程之间要想通信,如上图所示,每个线程在需要操作某个共享变量时,会将该主内存中这个共享变量拷贝一份副本存在在自己的本地内存(也叫工作内存,这里只是JMM的一个抽象概念,即将其笼统看做一片内存区域,用于每个线程存放变量,实际涉及到缓存,寄存器和其他硬件),线程操作这个副本,比如 int i = 1;一个线程想要进行 i++操作,会先将变量 i =1 的值先拷贝到自己本地内存操作,完成 i++,结果 i=2,此时主内存中的值还是1,在线程将结果刷新到主内存后,主内存值就更新为2,数据达到一致了。 如果线程A,线程B同时将 主内存中 i

Volatile semantics in C99

好久不见. 提交于 2019-12-04 17:32:23
问题 I have an issue with some low level code I am writing, I need to use objects as volatile, but it is not necessarily so that I want the types to be declared as volatile (for reusability reasons). I can however define pointer to a qualified variant of a structure as detailed in the following segment. struct x { int bar; }; struct x foobar; ... volatile struct x *foo = &foobar; Now foo is effectively a pointer to an object of the type: volatile struct x { volatile int x; }; since volatile apply

What does—or did—“volatile void function( … )” do?

我只是一个虾纸丫 提交于 2019-12-04 16:41:20
问题 I've seen How many usage does "volatile" keyword have in C++ function, from grammar perspective? about use of the volatile keyword on functions, but there was no clear explanation of what Case 1 from that question did. Only a statement by one of the respondents that it seemed pointless/useless. Yet I cannot quite accept that statement, since the AES software implementations for GNUC have been used for literally years, and they have a number of functions like this: INLINE volatile void

Volatile function

帅比萌擦擦* 提交于 2019-12-04 16:21:18
问题 Summary : What does the keyword volatile do when applied to a function declaration in C and in C++? Details : I see that it's possible to compile a function that is marked as volatile . However, I'm not sure what compiler optimization (if any) this prevents. For instance I created the following test case: volatile int foo() { return 1; } int main() { int total = 0; int i = 0; for(i = 0; i < 100; i++) { total += foo(); } return total; } When I compile with clang -emit-llvm -S -O3 test.c (gcc

Should a lock variable be declared volatile?

南笙酒味 提交于 2019-12-04 15:42:46
问题 I have the following Lock statement: private readonly object ownerLock_ = new object(); lock (ownerLock_) { } Should I use volatile keyword for my lock variable? private readonly volatile object ownerLock_ = new object(); On MSDN I saw that it usually used for a field that is accessed without locking, so if I use Lock I don't need to use volatile? From MSDN: The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize

Java Double Locking - Can someone explain more simply why intuition wouldn't work? [duplicate]

这一生的挚爱 提交于 2019-12-04 14:53:45
问题 This question already has answers here : Why is volatile used in double checked locking (6 answers) Closed 11 months ago . I found the following code here: http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java I am trying to understand why there are certain cases where this would not work. I read the explanation of the "subtle" problems, and that using volatile will fix the issue, but I'm a bit confused. // Broken multithreaded version // "Double-Checked Locking" idiom class Foo {

alternatives for volatile array

折月煮酒 提交于 2019-12-04 14:39:28
From other questions, I learned that the elements of a volatile array are not volatile. Only the reference itself is volatile. volatile[] int data; Thread A: data[4] = 457; Thread B: System.out.println(data[4]); Here, Thread B might never see the updated value. Which options/alternatives do I have to achieve the same thing? I would like to avoid having to synchronize the array, since it is hardly ever altered. However, it is being read a lot by some threads. Synchronizing it will very likely lower the throughput, which is very important in this example. Is my only option a copy-on-write data

how does the volatile count++ operation be made thread safe

房东的猫 提交于 2019-12-04 14:31:48
I have been going through JCIP and there the author says.. A special case of thread confinement applies to volatile variables. It is safe to perform read-modify-write operations on shared volatile variables as long as you ensure that the volatile variable is only written from a single thread For instance count++ is considered a compound operation (reading the value,adding one to it,and updating the value) and declaring count as volatile does not make this operation atomic, so thread safety is not guaranteed here !! Am i right ?? But here the author says we can fix it if we ensure that the

深入CAS的底层实现机制,以及对应的使用风险

独自空忆成欢 提交于 2019-12-04 14:18:50
概述 CAS(Compare-and-Swap),即比较并替换,是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术。CAS也是现在面试经常问的问题,本文将深入的介绍CAS的原理。 案例 介绍CAS之前,我们先来看一个例子。 /** * @author joonwhee * @date 2019/7/6 */ public class VolatileTest { public static volatile int race = 0; private static final int THREADS_COUNT = 20; public static void increase() { race++; } public static void main(String[] args) throws InterruptedException { Thread[] threads = new Thread[THREADS_COUNT]; for (int i = 0; i < THREADS_COUNT; i++) { threads[i] = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10000; i++) { increase(); } } })