使用C / Pthreads中:做共享变量需要挥发? [英] Using C/Pthreads: do shared variables need to be volatile?

查看:182
本文介绍了使用C / Pthreads中:做共享变量需要挥发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C编程语言和pthread的线程库;这样做是线程之间共享的变量/结构需要被声明为volatile?假设他们可能会被锁的保护与否(障碍也许)。

In the C programming language and Pthreads as the threading library; do variables/structures that are shared between threads need to be declared as volatile? Assuming that they might be protected by a lock or not (barriers perhaps).

请问pthread的POSIX标准有什么说这个,就是这个编译器依赖或没有?

Does the pthread POSIX standard have any say about this, is this compiler-dependent or neither?

编辑补充:感谢伟大的答案。但是,如果你的的使用锁;如果你正在使用 的障碍,例如?或使用原语,如比较和交换直接和原子修改共享变量code ...

Edit to add: Thanks for the great answers. But what if you're not using locks; what if you're using barriers for example? Or code that uses primitives such as compare-and-swap to directly and atomically modify a shared variable...

推荐答案

我觉得挥发性一个非常重要的特性是,它使修改时,每次访问时间变量被写入到内存中,并从内存中读取信息。其他答案在这里混的挥发性和同步,这是一些其他的答案比这清楚地表明挥发性不同步原语(信贷,信贷是由于)。

I think one very important property of volatile is that it makes the variable be written to memory when modified, and reread from memory each time it accessed. The other answers here mix volatile and synchronization, and it is clear from some other answers than this that volatile is NOT a sync primitive (credit where credit is due).

但除非你使用volatile,编译器是免费的缓存在任何时间长度的寄存器共享数据...如果你希望自己的数据被写入到是predictably写入实际内存,而不仅仅是在其自由裁量权的编译器在寄存器缓存,您需要将其标记为波动。另外,如果你只有你离开一个函数修改后访问共享数据时,你可能会被罚款。不过,我建议不是靠盲目的运气来确保值从寄存器写回内存。

But unless you use volatile, the compiler is free to cache the shared data in a register for any length of time... if you want your data to be written to be predictably written to actual memory and not just cached in a register by the compiler at its discretion, you will need to mark it as volatile. Alternatively, if you only access the shared data after you have left a function modifying it, you might be fine. But I would suggest not relying on blind luck to make sure that values are written back from registers to memory.

特别是在丰富的寄存器机器(即,不是86),变量可以住在寄存器很长时间,和良好的编译器可以缓存在寄存器结构或整个结构,甚至部分。所以,你应该使用volatile,但是对于性能,也值复制到为计算局部变量,然后做一个明确的回写。从本质上讲,有效地利用波动意味着做了一些负载店思考在C code。

Especially on register-rich machines (i.e., not x86), variables can live for quite long periods in registers, and a good compiler can cache even parts of structures or entire structures in registers. So you should use volatile, but for performance, also copy values to local variables for computation and then do an explicit write-back. Essentially, using volatile efficiently means doing a bit of load-store thinking in your C code.

在任何情况下,你必须积极使用某种类型的操作系统级别的提供的同步机制,以创建一个正确的程序。

In any case, you positively have to use some kind of OS-level provided sync mechanism to create a correct program.

有关的挥发性疲软的一个例子,请参阅 http://jakob.engbloms.se/archives/我德克尔的算法的例子65 ,这证明pretty以及挥发性不起作用同步。

For an example of the weakness of volatile, see my Decker's algorithm example at http://jakob.engbloms.se/archives/65, which proves pretty well that volatile does not work to synchronize.

这篇关于使用C / Pthreads中:做共享变量需要挥发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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