Subj : Re: [.NET] Volatile Fields [correction] To : comp.programming.threads From : David Hopwood Date : Mon Sep 19 2005 04:16 pm Cool Guy wrote: > David Hopwood wrote: >=20 >>For instance, you might think that in >> this example: >> >> Start with Y =3D=3D Z =3D=3D 0. >> >> Processor 1: Y =3D 1; >> Processor 2: if (Y =3D=3D 1) { Z =3D 1; } >> Processor 3: if (Z =3D=3D 1) { assert(Y =3D=3D 1); } >> >> the assert cannot fail (because Z =3D 1 can only happen after Y =3D 1= ). >> But in some memory models, including the model used by x86 >> (as far as it's possible to tell from the documentation), and probabl= y also the >> one used by .NET, it *is* possible for the assert to fail. >=20 > How comes? The write to Y is not performed as a single atomic event; it has to be mo= delled as two events: "Y =3D 1 is performed with respect to processor 2" and "Y = =3D 1 is performed with respect to processor 3". So it can happen that processor 2= sees Y =3D=3D 1 before writing Z =3D 1, but processor 3 does not see Y =3D=3D = 1 until after it sees Z =3D=3D 1. If it helps, think of each write as causing separate asynchronous message= s to be sent from the writing processor to each other processor. The memory mo= del puts some constraints on the ordering of these messages; for example, in "processor consistency" models, if processor A makes several writes, they= will be seen in the same order by some other processor B. This corresponds to (one-to-one) FIFO ordering in a message passing system [CMT96]. However, = in the example above this constraint doesn't help, because no processor make= s more than one write. Here's a space-time diagram showing an execution in which the above examp= le asserts. ('*' shows message reception. Time goes from left to right. View= in a fixed width font.) P1 --(Y =3D 1)----------------------------------------------- \\__________________________________ \ \ P2 -------*--(Y =3D=3D 1)--(Z =3D 1)-------------- \ ------------ \ \ \ \ P3 -------------------------*-(Z =3D=3D 1)-(Y =3D=3D 0)-*-(Y =3D=3D 1)- [CMT96] Bernadette Charron=96Bost, Friedemann Mattern, Gerard Tel Synchronous, asynchronous, and causally ordered communication --=20 David Hopwood .