Subj : Re: Memory visibility and MS Interlocked instructions To : comp.programming.threads From : Torsten Robitzki Date : Mon Aug 29 2005 08:12 pm Scott Meyers wrote: > What confuses me is that here only the writer needs to take an action to > guarantee that readers will see things in the proper order, where my > understanding had been that the reader, too, would have to take some action > before reading iValue and fValueHasBeenComputed to ensure that it didn't > get a stale value for one or both. > > Obviously I'm missing something. Can somebody please clear up my > confusion? it realy depends on the architecture and the compiler/threading library. If you have a architecture that only reorders writes, you have to emit a membar() between the write to the data and the write to the flag. Otherwise othere CPUs will not be able to see the changes to the data and flag in the given order. The memory barrier will tell the hardware to not reorder writes to memory across this membar. In this case it will be sufficient to make sure that the compiler won't reorder the reads on the reading side, which might be given as one of the variables involed beeing volatile qualified. If the hardware reorders read and writes one have to make sure that reads aren't reordered by the hardware too. regards Torsten .