volatile

C# ThreadStatic + volatile members not working as expected

人盡茶涼 提交于 2019-12-23 09:39:12
问题 I was reading through the tips and tricks post and I thought I'd try out some of the C# stuff that I'd never done before. Therefore, the following code serves no actual purpose, but is just a 'test function' to see what happens. Anyway, I have two static private fields: private static volatile string staticVolatileTestString = ""; [ThreadStatic] private static int threadInt = 0; As you can see, I'm testing ThreadStaticAttribute and the volatile keyword. Anyway, I have a test method that looks

c++ volatile multithreading variables

点点圈 提交于 2019-12-23 09:34:15
问题 I'm writing a C++ app. I have a class variable that more than one thread is writing to. In C++, anything that can be modified without the compiler "realizing" that it's being changed needs to be marked volatile right? So if my code is multi threaded, and one thread may write to a var while another reads from it, do I need to mark the var volaltile? [I don't have a race condition since I'm relying on writes to ints being atomic] Thanks! 回答1: C++ hasn't yet any provision for multithreading. In

Do I need this field to be volatile?

℡╲_俬逩灬. 提交于 2019-12-23 07:37:15
问题 I have a thread that spins until an int changed by another thread is a certain value. int cur = this.m_cur; while (cur > this.Max) { // spin until cur is <= max cur = this.m_cur; } Does this.m_cur need to be declared volatile for this to work? Is it possible that this will spin forever due to compiler optimization? 回答1: Yes, that's a hard requirement. The just-in-time compiler is allowed to store the value of m_cur in a processor register without refreshing it from memory. The x86 jitter in

Why using “volatile” does not show any difference here?

南楼画角 提交于 2019-12-23 05:23:14
问题 I am learning the usage of volatile in Java. Here is a sample code I read from many articles: static volatile boolean shutdownRequested = false; ... public void shutdown() { shutdownRequested = true; } public void doWork() { while (!shutdownRequested) { // do stuff } } I try this on my machine with and without "volatile", but they show no difference: they can both shutdown. So what's wrong? Is there anything wrong with my code, or does it depend on the version of the Java compiler? Addition:

volatile variables or volatile structure problems in C

对着背影说爱祢 提交于 2019-12-23 04:48:17
问题 Here is my header file (Header.h) #include <stdio.h> #include <string.h> void Process(void); and "Header.C" #include <stdio.h> #include <string.h> #include "Header.h" struct St{ unsigned long int volatile Var1; unsigned long int volatile Var2; unsigned char volatile Flag; }; extern struct ST Variable; void Process(void){ Variable.Var1=Variable.Var2; } and Main file: #include <stdio.h> #include <string.h> #include "Header.h" struct St{ unsigned long int volatile Var1; unsigned long int

java 内存模型

隐身守侯 提交于 2019-12-23 02:41:03
java 内存模型 文章目录 java 内存模型 计算机内存模型 并发编程特性 Java Memory Model JMM 的实现 原子性 可见性 有序性 计算机内存模型 当程序在运行过程中,会将运算需要的数据从主存复制一份到 CPU 的高速缓存当中,那么 CPU 进行计算时就可以直接从它的高速缓存读取数据和向其中写入数据,当运算结束之后,再将高速缓存中的数据刷新到主存当中 当 CPU 要读取一个数据时,首先从一级缓存中查找,如果没有找到再从二级缓存中查找,如果还是没有就从三级缓存或内存中查找 在 CPU 和主存之间增加缓存,在多线程场景下就可能存在 缓存一致性问题 ,也就是说,在多核 CPU 中,每个核的自己的缓存中,关于同一个数据的缓存内容可能不一致 为了使处理器内部的运算单元能够尽量的被充分利用,处理器可能会对输入代码进行乱序执行处理。这就是 处理器优化 除了现在很多流行的处理器会对代码进行优化乱序处理,很多编程语言的编译器也会有类似的优化,比如 Java 虚拟机的即时编译器(JIT)也会做 指令重排 并发编程特性 原子性 是指在一个操作中就是 cpu 不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行 可见性 是指当多个线程访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值 有序性 即程序执行的顺序按照代码的先后顺序执行

JAVA并发--volatile

廉价感情. 提交于 2019-12-22 17:10:32
学过计算机组成原理的一定知道,为了解决内存速度跟不上CPU速度这个问题,在CPU的设计中加入了缓存机制,缓存的速度介于CPU和主存之间。在进行运算的时候,CPU将需要的数据映射一份在缓存中,然后直接操作位于缓存中的数据,操作完毕后再将缓存中的数据写回到主存。这在单线程环境中是没有任何问题的。但是在多线程环境中就大不同了。 假设现在有这样的一个场景:有两个线程thread1和thread2,他们都在操作位于主存上的一个数据int a=2(具体操作为读取a的值并执行一个自增操作)。逻辑上正确的结果:应当是最后a=4。但可能有这样的情况,thread1将a=2从主存映射到自己的工作内存上,自增后变成a=3,在将a=3从工作内存写回到主存之前,thread2也将a=2从从主存映射到自己的工作内存上,也自增后变成a=3。然后两个线程先后将a=3写回到主存上。显然,a=3不是我们想看到的。看,这就是一个常见的缓存一致性问题。两个线程对a的操作结果互不可见,thread1不知道thread2对a进行了自增,thread2也不知道thread1对a进行了自增。在多线程编程中就是会出现这样一致性的问题。(在JMM中,可以知道,内存分为主内存和工作内存 ,每个线程有自己 的工作内存,他们共享主内存 )。 因此我们要办法让线程对共享变量的操作结果互相可见

IPC via mmap'ed file: should atomics and/or volatile be used?

怎甘沉沦 提交于 2019-12-22 13:46:57
问题 I use a mmap'ed file to share data between processes. The code is like this: struct Shared { int Data; }; int file = open("file.dat", O_RDWR); Shared* shared = static_cast<Shared*>( mmap(0, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, file, 0)); shared->Data++; The questions are: Should I use volatile qualifier ( volatile int Data )? Should I use atomic operations on the shared data ( __sync_fetch_and_add(&(shared->Data), 1) )? For future reference: Volatile: Almost

Why does the compiler optimize away shared memory reads due to strncmp() even if volatile keyword is used?

好久不见. 提交于 2019-12-22 09:57:09
问题 Here is a program foo.c that writes data to shared memory. #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <stdint.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h> int main() { key_t key; int shmid; char *mem; if ((key = ftok("ftok", 0)) == -1) { perror("ftok"); return 1; } if ((shmid = shmget(key, 100, 0600 | IPC_CREAT)) == -1) { perror("shmget"); return 1; } printf("key: 0x%x; shmid: %d\n", key, shmid); if ((mem = shmat(shmid, NULL, 0))

assignment expressions and volatile

五迷三道 提交于 2019-12-22 09:46:40
问题 I seem to have a reasonable understanding of volatiles in general, but there's one seemingly obscure case, in which I'm not sure how things are supposed to work per the standard. I've read the relevant parts of C99 and a dozen or more related posts on SO, but can't find the logic in this case or a place where this case is explained. Suppose we have this piece of code: int a, c; volatile int b; a = b = 1; c = b += 1; /* or equivalently c = ++b; */ Should a be evaluated like this: b = 1; a = b;