由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to use volatile in c++?
相关主题
About volatile in CC signal SIGFPE 问题
C arrayc++ template是不是跟Macro一个道理?
有谁知道怎么把matlab和VC联接着一起用? (转载)第五版的 c++ primer 出来了
Think Python用volatile退出线程对不对?(C++)
register variablequestion about volatile variable on multiple cores
Help: Another C++ compilation error on GCCVolatile variables do not provide any atomicity (转载)
C++ questionsize of structure
关于signal handlerwhy int** cannot convert to const int** ?
相关话题的讨论汇总
话题: volatile话题: variables话题: use话题: c++话题: memory
进入Programming版参与讨论
1 (共1页)
e****d
发帖数: 895
1
Does anyone have experience in using volatile variables as
memory barriers for lock free structures in c++?
X****r
发帖数: 3557
2
volatile is not a memory barrier. Don't use it for that.

【在 e****d 的大作中提到】
: Does anyone have experience in using volatile variables as
: memory barriers for lock free structures in c++?

e****d
发帖数: 895
3
It depends on compilers. MSVC does give volatile variables
acquire/release semantics. GCC might also do. But c++0x
will introduce atomic variables to replace this usage.

【在 X****r 的大作中提到】
: volatile is not a memory barrier. Don't use it for that.
k******r
发帖数: 2300
4
volatile variable 好像不能做到这一点,你有这样的例子吗?volatile variable 只
是可以用来 turn off 一些 compiler optimization。

【在 e****d 的大作中提到】
: Does anyone have experience in using volatile variables as
: memory barriers for lock free structures in c++?

a****l
发帖数: 8211
5
why do you think volatile can be used as memory barrier?

【在 e****d 的大作中提到】
: Does anyone have experience in using volatile variables as
: memory barriers for lock free structures in c++?

e****d
发帖数: 895
6
Volatile variables disable caching and the order between volatile
variables is guaranteered. Some compilers do also give acquire/
release semantics to volatile variables although it's not standard.
It's obvious not portable and not recommended. I never use it
in this way, just curious anyone does.

【在 a****l 的大作中提到】
: why do you think volatile can be used as memory barrier?
a****l
发帖数: 8211
7
http://en.wikipedia.org/wiki/Memory_barrier
this article addresses this issues well.

【在 e****d 的大作中提到】
: Volatile variables disable caching and the order between volatile
: variables is guaranteered. Some compilers do also give acquire/
: release semantics to volatile variables although it's not standard.
: It's obvious not portable and not recommended. I never use it
: in this way, just curious anyone does.

c***a
发帖数: 84
8
general rule about volatile: Don't use it.
general rule about implementing your own lock free data structure: Don't do
it.

【在 e****d 的大作中提到】
: Does anyone have experience in using volatile variables as
: memory barriers for lock free structures in c++?

e****d
发帖数: 895
9
Not true. At least you need to use volatile variables when
they could be updated by signal handlers.

do

【在 c***a 的大作中提到】
: general rule about volatile: Don't use it.
: general rule about implementing your own lock free data structure: Don't do
: it.

t****t
发帖数: 6806
10
you should use atomic for that...

【在 e****d 的大作中提到】
: Not true. At least you need to use volatile variables when
: they could be updated by signal handlers.
:
: do

相关主题
Help: Another C++ compilation error on GCCC signal SIGFPE 问题
C++ questionc++ template是不是跟Macro一个道理?
关于signal handler第五版的 c++ primer 出来了
进入Programming版参与讨论
e****d
发帖数: 895
11
Will atomic variable tell the cpu not to put it in
memory cache?

【在 t****t 的大作中提到】
: you should use atomic for that...
c***a
发帖数: 84
12
Volatile usually has nothing to do with memory cache.
The point is, one almost certainly doesn't NEED to use volatile, at least
not directly in her code. We should stick to operations (atomics, locks, etc
.) whose semantics are easier to understand and reason with.
Same thing with lock free data structures. They may look fancy, but they are
very difficult to get right, usually non-portable, and often perform worse
than locks.
This is not to say volatile and lock free data structures should never be
used. It's just that those cases are very, very rare, especially for an
application programmer.

【在 e****d 的大作中提到】
: Will atomic variable tell the cpu not to put it in
: memory cache?

a****l
发帖数: 8211
13
这是不可能的.cpu永远是从cache里读数据的,区别仅在于什么时候更新cache里的数据.

【在 e****d 的大作中提到】
: Will atomic variable tell the cpu not to put it in
: memory cache?

e****d
发帖数: 895
14
That's right. Data might always go through the cache. But look ahead
and delay writeback will not apply to them. I think volatile
will tell the cpu optimization not to do lookahead and delay writeback.

据.

【在 a****l 的大作中提到】
: 这是不可能的.cpu永远是从cache里读数据的,区别仅在于什么时候更新cache里的数据.
e****d
发帖数: 895
15
volatile will be used if a variable can be updated asynchronously.
Lockfree is critical for some high performance requirements.
I agree people can easily make mistakes when implementing lockfree.

etc
are
worse

【在 c***a 的大作中提到】
: Volatile usually has nothing to do with memory cache.
: The point is, one almost certainly doesn't NEED to use volatile, at least
: not directly in her code. We should stick to operations (atomics, locks, etc
: .) whose semantics are easier to understand and reason with.
: Same thing with lock free data structures. They may look fancy, but they are
: very difficult to get right, usually non-portable, and often perform worse
: than locks.
: This is not to say volatile and lock free data structures should never be
: used. It's just that those cases are very, very rare, especially for an
: application programmer.

c***a
发帖数: 84
16
For very few limited cases, yes. In general, no if you are already using
other synchronization operations.

【在 e****d 的大作中提到】
: volatile will be used if a variable can be updated asynchronously.
: Lockfree is critical for some high performance requirements.
: I agree people can easily make mistakes when implementing lockfree.
:
: etc
: are
: worse

1 (共1页)
进入Programming版参与讨论
相关主题
why int** cannot convert to const int** ?register variable
[合集] iterator & const_iteratorHelp: Another C++ compilation error on GCC
A C++ compiler related interview questionC++ question
面试题还是不太清楚怎么回答。尤其是8,我用MSVC试了一下,没有问题啊。关于signal handler
About volatile in CC signal SIGFPE 问题
C arrayc++ template是不是跟Macro一个道理?
有谁知道怎么把matlab和VC联接着一起用? (转载)第五版的 c++ primer 出来了
Think Python用volatile退出线程对不对?(C++)
相关话题的讨论汇总
话题: volatile话题: variables话题: use话题: c++话题: memory