可以编译器有时缓存变量声明为volatile [英] Can compiler sometimes cache variable declared as volatile

查看:168
本文介绍了可以编译器有时缓存变量声明为volatile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,编译器优化从未被声明为可变挥发性。不过,我宣布一个这样的数组。

 挥发性多头排列; [8]

和不同的线程读取和写入。的数组的元素仅由一个线程修改和任何其他线程读取。然而,在某些情况下,我已经注意到,即使我从一个线程修改元素,该线程读取它没有注意到的变化。它使在阅读同样的老值,则编译器缓存的地方吧。但是编译器校长不应该缓存volatile变量,对不对?那么怎么来这种情况正在发生。

注意:我不使用挥发性线程同步,所以请别再给我解答,如使用锁定或原子变量。我知道挥发,原子变量和互斥之间的差异。另外请注意,该架构的x86其中有主动缓存一致性。我也看了变量足够长的时间后,它被认为可以通过其他线程修改。即使经过很长一段时间,读线程不能看到修改后的值。


解决方案

  

但是编译器校长不应该缓存volatile变量,对吧?


没有,原则上编译器必须读/每次读/写变量时间写变量的地址。

在C ++ 03,它根本不考虑线程,它是由执行来定义什么是访问地址是指在一个线程中运行时。像这样的细节被称为记忆模式。 pthreads的,例如,允许每个线程缓存全体存储器,包括易失性变量。 IIRC,MSVC提供了保证,适当大小的挥发性变量是原子的,它会避免缓存(而,它将尽可能刷新为所有内核的单一连贯缓存)。它提供了保证的原因是因为它的合理的便宜,英特尔这样做的 - 仅适用于Windows真正关心的基于英特尔处理器的架构,而Posix的关注本身更奇特的东西。

C ++ 11定义了线程内存模型,它说,这是一个数据的比赛(即挥发性的保证在一个线程的读取,相对于在另一个线程写测序)。两个访问可以在一个特定的顺序进行排序,在未指定的顺序排序(标准可能会说不确定的顺序,我记不清了),还是不排序的。在所有未测序是坏的 - 如果以下两种unsequenced的访问是一个写那么行为是不确定的。

这里的关键是隐含的,然后在我从一个线程修改一个元素,然后线程读取它没有注意到的改变。你假设的操作序列,但他们没有。至于读线程而言,除非你使用某种同步在另一个线程写入并不一定发生过的。而实际上它比那更糟糕 - 你可能会想从我只是写,这只是这是不确定的操作顺序,但实际上与数据种族程序的行为是不确定的。

From what I know, the compiler never optimizes a variable that is declared as volatile. However, I have an array declared like this.

volatile long array[8];

And different threads read and write to it. An element of the array is only modified by one of the threads and read by any other thread. However, in certain situations I've noticed that even if I modify an element from a thread, the thread reading it does not notice the change. It keeps on reading the same old value, as if compiler has cached it somewhere. But compiler in principal should not cache a volatile variable, right? So how come this is happening.

NOTE: I am not using volatile for thread synchronization, so please stop giving me answers such as use a lock or an atomic variable. I know the difference between volatile, atomic variables and mutexes. Also note that the architecture is x86 which has proactive cache coherence. Also I read the variable for long enough after it is supposedly modified by the other thread. Even after a long time, the reading thread can't see the modified value.

解决方案

But compiler in principal should not cache a volatile variable, right?

No, the compiler in principle must read/write the address of the variable each time you read/write the variable.

[Edit: At least, it must do so up to the point at which the the implementation believes that the value at that address is "observable". As Dietmar points out in his answer, an implementation might declare that normal memory "cannot be observed". This would come as a surprise to people using debuggers, mprotect, or other stuff outside the scope of the standard, but it could conform in principle.]

In C++03, which does not consider threads at all, it is up to the implementation to define what "accessing the address" means when running in a thread. Details like this are called the "memory model". Pthreads, for example, allows per-thread caching of the whole of memory, including volatile variables. IIRC, MSVC provides a guarantee that volatile variables of suitable size are atomic, and it will avoid caching (rather, it will flush as far as a single coherent cache for all cores). The reason it provides that guarantee is because it's reasonably cheap to do so on Intel -- Windows only really cares about Intel-based architectures, whereas Posix concerns itself with more exotic stuff.

C++11 defines a memory model for threading, and it says that this is a data race (i.e. that volatile does not ensure that a read in one thread is sequenced relative to a write in another thread). Two accesses can be sequenced in a particular order, sequenced in unspecified order (the standard might say "indeterminate order", I can't remember), or not sequenced at all. Not sequenced at all is bad -- if either of two unsequenced accesses is a write then behavior is undefined.

The key here is the implied "and then" in "I modify an element from a thread AND THEN the thread reading it does not notice the change". You're assuming that the operations are sequenced, but they're not. As far as the reading thread is concerned, unless you use some kind of synchronization the write in the other thread hasn't necessarily happened yet. And actually it's worse than that -- you might think from what I just wrote that it's only the order of operations that is unspecified, but actually the behavior of a program with a data race is undefined.

这篇关于可以编译器有时缓存变量声明为volatile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆