Subj : Re: [.NET] Volatile Fields To : comp.programming.threads From : David Hopwood Date : Fri Sep 16 2005 10:46 pm Cool Guy wrote: > See the example on > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_4_3.asp > - or - > http://tinyurl.com/7gml4 > > Shouldn't Test.result be declared as volatile as well? It doesn't need to be. Because 'finished' is volatile, the assignment "finished = true;" causes previous writes by Thread2 (whether or not they are to volatile variables) to be "performed" before the write to 'finished'. This is what the page means by 'A volatile write has "release semantics"'. In addition, the access to 'finished' in "if (finished) ..." guarantees that writes performed before 'finished' became true are visible to the Main thread. This is what the page means by 'A volatile read has "acquire semantics"'. The combination of these two barriers guarantees that the result is 143 (either on its own would not be enough). -- David Hopwood .