Re: Share .cpp and .h along projects



On Thu, 16 Aug 2007 16:30:28 -0500, "Ben Voigt [C++ MVP]"
<rbv@xxxxxxxxxxxxx> wrote:

Only single machine words can be used for interlocked operations, but any
larger object can be controlled in a threadsafe manner using a volatile
pointer (which is word-sized) and memory barriers.

That's nonsense, but simplify it to a uniprocessor system, which does not
have memory barriers. Heck, you could even limit it to x86 SMP systems.
Explain how you can use (say) a std::vector in a thread-safe way by "using
a volatile pointer".

Note that people who actually understand how to do multithreaded
programming and use synchronization objects such as mutexes rarely if ever
use volatile and may not even have heard of memory barriers, which they
don't need to know about in order to use synchronization objects.

<snip>

Compiler optimizations (and CPU reorders) are always defined in terms of
equivalence on a single sequence of execution.

Some of those optimizations cannot be safely applied by a compiler intended
to be useful for multithreaded programming. For some reason, that point is
not getting across.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Another ANSI C question about volatile
    ... > The cast is on the address of x. ... > lvalue of type 'pointer to volatile int'. ... > volatile, ... > that a compiler, needing to reread xp each time, would then ...
    (comp.lang.c)
  • Re: Share .cpp and .h along projects
    ... Because myarray is a pointer-to-const inside a const member function ... pointer, not an array. ... volatile pointer, not a pointer-to-volatile: ... protecting it against simple assignment, ...
    (microsoft.public.vc.language)
  • Re: MSDN volatile sample
    ... must be specified to get the memory barriers. ... Telling us we have to put volatile keyword to thread sharing data ... Now I strongly suspect whether compiler will generate any wrong code -- ... It seems that volatile will make wrong optimization to prevent thread1 ...
    (microsoft.public.vc.language)
  • Re: i386 nmi_watchdog: Merge check_nmi_watchdog fixes from x86_64
    ... > after the store without volatile it seems a reasonable ... > as we have taken the address earlier so at some point the compiler ... pointer away and will be referring to it later. ... or if the compiler does whole-program optimization and can see ...
    (Linux-Kernel)
  • Re: Share .cpp and .h along projects
    ... Because myarray is a pointer-to-const inside a const member function ... pointer, not an array. ... volatile pointer, not a pointer-to-volatile: ... Since you admit that InterlockedXXX ...
    (microsoft.public.vc.language)