Subj : Re: [.NET] Volatile Fields [correction] To : comp.programming.threads From : David Hopwood Date : Mon Sep 19 2005 08:08 pm Cool Guy wrote: > David Hopwood wrote: > >>The write to Y is not performed as a single atomic event; it has to be modelled >>as two events: "Y = 1 is performed with respect to processor 2" and "Y = 1 is >>performed with respect to processor 3". So it can happen that processor 2 sees >>Y == 1 before writing Z = 1, but processor 3 does not see Y == 1 until after >>it sees Z == 1. > > > > So it's impossible to make such code thread-safe, then? Application code should typically be written to use higher-level abstractions such as locks, message passing, or transactional memory. Examples like this one usually only arise when trying to implement such abstractions. It is quite possible to work around problems like the one in this example, if needed. It just may not be sufficient to use memory barriers; sometimes you need interlocked instructions or equivalent. (Recently, some people have been promoting the use of lock-free data structures for application programming. That's a bad idea, because it requires an understanding of memory models that most application programmers don't have. Nor would it be an efficient use of those programmers' time to gain sufficient understanding to be able to maintain or fix libraries of lock-free structures. If abstractions that need to be implemented in terms of a memory model are provided by an OS, thread library, or language implementation, OTOH, then application programmers are not expected to need to understand, maintain, or fix them; that is explicitly someone else's problem.) -- David Hopwood .