volatile keyword

G

Graeme Prentice

According to this web page
http://www.jaggersoft.com/csharp_standard/17.4.3.htm

making data volatile results in ordered reads and writes. Does this
mean that the following (pseudo) code should work (assuming flag1 and
flag2 are volatile)

// thread 1
flag1 = true;
if ( !flag2 )
// perform an operation that needs to be serialised
else
flag1 = false; // try again



// thread 2
flag2 = true;
if ( !flag1 )
// perform an operation that needs to be serialised
else
flag2 = false; // try again


I'm not suggesting to use this code, I just want to know what volatile
does.

If this code works, are special instructions used to ensure that
re-ordering of the reads and writes doesn't occur? What would these
instructions be on X86? Is cache coherency guaranteed on all Windows/C#
platforms?

Graeme
 
M

Miha Markic [MVP C#]

Hi Grame,

No, it won't work.
Volatile means only that the varaible is synchronized among processors if
there are two or more processors (otherwise one the value might differ
between processors) and you don't use locking mechanisms.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top