Re: Native version of VolatileRead and VolatileWrite

Peter Ritchie <[email protected]> Thu, 13 Dec 2007 09:30:09 -0500
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <LISTSERV%[email protected]>
For a single function, InterlockedExchange most closely matches
VolatileRead/Write.

On an x86 system, you can simply use assignment.  On some other processor
types it's the memory barrier that is the important side-effect.
InterlockedExchange gives you that memory barrier.  You don't need to use
the variable directly with InterlockedExchange to get the memory barrier.
You could write VolatileRead/Write like this in native C++:

class Thread
{
public:
 static int VolatileRead(volatile int & value)
 {
  MemoryBarrier();
  return value;
 }

 static void VolatileWrite(volatile int & destination, int value)
 {
  MemoryBarrier();
  destination = value;
 }

 static void MemoryBarrier()
 {
  volatile LONG value;
  InterlockedExchange(&value, 0);
 }
};

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com