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
|
|
|
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
|