C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to use volatile key word?
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bo Persson
Guest





PostPosted: Sun Jul 01, 2012 12:59 pm    Post subject: Re: how to use volatile key word? Reply with quote



Dombo skrev 2012-06-28 22:26:
Quote:
Op 28-Jun-12 21:16, Ian Collins schreef:
On 06/28/12 08:46 PM, Juha Nieminen wrote:
Ian Collins<ian-news (AT) hotmail (DOT) com> wrote:
Anyways, if the choice is between writing 'volatile' and writing
several
lines of code to implement locking in a situation where locking is not
needed, the choice is pretty clear.

Until you have to run on a multi-core system....

What difference would that make?

On reflection, possibly not a lot. volatile should keep the variable out
of caches and visible to the other cores.

I don't know what the C++ 11 standard says about this but, on VS2010 it
does nothing special to keep volatile variables out of the cache.


That's because VS2010 only targets platforms where the hardware does the
cache sync. So it doesn't have to do anything.

It also means that the code can easily be non-portable, because "works
on x86" doesn't mean "works elsewhere".


Bo Persson
Back to top
Nobody
Guest





PostPosted: Sun Jul 01, 2012 2:05 pm    Post subject: Re: how to use volatile key word? Reply with quote



On Sun, 01 Jul 2012 16:59:44 +0200, Bo Persson wrote:

Quote:
On reflection, possibly not a lot. volatile should keep the variable out
of caches and visible to the other cores.

I don't know what the C++ 11 standard says about this but, on VS2010 it
does nothing special to keep volatile variables out of the cache.

That's because VS2010 only targets platforms where the hardware does the
cache sync. So it doesn't have to do anything.

It's more because "volatile" is normally understood as requesting that the
compiler doesn't re-order or coalesce load/store operations, not that it
should provide workarounds for re-ordering performed by the CPU. If you
want the latter, you need to use explicit (and platform-specific) fence
operations.

Quote:
It also means that the code can easily be non-portable, because "works
on x86" doesn't mean "works elsewhere".

x86 has one of the strongest memory consistency models. At the other
extreme, Alpha, PPC, IA-64 and ARM have very weak consistency.

Coupled with the lack of any alignment constraints, x86 is about the worst
test platform if you want portable code, as it papers over the most common
issues. On the upside, being little-endian means that omitting
host-order/network-order conversions will show up.
Back to top
88888 Dihedral
Guest





PostPosted: Mon Jul 02, 2012 5:44 am    Post subject: Re: how to use volatile key word? Reply with quote



Nobody於 2012年7月2日星期一UTC+8上午12時05分15秒寫道:
Quote:
On Sun, 01 Jul 2012 16:59:44 +0200, Bo Persson wrote:

On reflection, possibly not a lot. volatile should keep the variable out
of caches and visible to the other cores.

I don't know what the C++ 11 standard says about this but, on VS2010 it
does nothing special to keep volatile variables out of the cache.

That's because VS2010 only targets platforms where the hardware does the
cache sync. So it doesn't have to do anything.

It's more because "volatile" is normally understood as requesting that the
compiler doesn't re-order or coalesce load/store operations, not that it
should provide workarounds for re-ordering performed by the CPU. If you
want the latter, you need to use explicit (and platform-specific) fence
operations.

It also means that the code can easily be non-portable, because "works
on x86" doesn't mean "works elsewhere".

x86 has one of the strongest memory consistency models. At the other
extreme, Alpha, PPC, IA-64 and ARM have very weak consistency.

Coupled with the lack of any alignment constraints, x86 is about the worst
test platform if you want portable code, as it papers over the most common
issues. On the upside, being little-endian means that omitting
host-order/network-order conversions will show up.

The X86 memory related instructions were designed in the period that
the DRAM chips were expensive in the consumer markete in the 70's.

But Intel did support the virtual mode long time ago.
Back to top
Stuart Redmann
Guest





PostPosted: Mon Jul 02, 2012 8:06 am    Post subject: Re: how to use volatile key word? Reply with quote

On 28 Jun., Scott Lurndal wrote:

[snipped discussion about proper use of keyword "volatile"]

Quote:
The caching hardware itself ensures that the caches on all cores/sockets are
coherent, such that there will only be one core that holds a modified
value of the memory line (64-bytes for most) and all other cores will invalidate any
copies of the cache line.

[snip]
Quote:

volatile
has nothing to do with the hardware architecture - it is simply a directive to
the compiler that ensures that every reference results in a corresponding load
instruction, and every modification results in a corresponding store instruction.

[snip]

I think that this is pretty much the answer to Paavo's question
whether the C++ standard guarantees this behaviour. Do you happen to
know chapter and verse of the C++ standard?

Thanks in advance,
Stuart
Back to top
Philip Lantz
Guest





PostPosted: Thu Aug 02, 2012 10:35 pm    Post subject: Re: how to use volatile key word? Reply with quote

Scott Lurndal wrote:
Quote:
Fortunately, on x86 anyway, loads from the integral data types (byte,
word, doubleword and extended doubleword (8, 16, 32 and 64)) are always
atomic without the LOCK prefix.

However, if the atomic being loaded is related to some other variable (e.g.
the count
of entries in a queue and the queue listhead), then the LOCK prefix should
be used on loads of the atomic value to ensure that all prior stores are
completed before the load issues.

The LOCK prefix can be used only on read-modify-write instructions with
a memory destination. It cannot be used on a load instruction. (You
could use a locked xadd or cmpxchg to load a value; is that what you
meant?)
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
 


Powered by phpBB © 2001, 2006 phpBB Group