[concurrency-interest] concurrency puzzle
brian at quiotix.com
Tue Sep 12 17:02:50 EDT 2006
> It seems my understanding of cache wasn't correct.
Part of the problem with the old memory model was that it was cast in
the languages of caching. Such a mental model may well be more
confusing than helpful, as the duration of this thread suggests.
A memory model speaks to the problem of knowing when a memory write by
thread X is guaranteed to be visible to a memory read by thread Y.
Talking about cache flushes doesn't fully address that problem.
There are lots of reasons why a memory write by thread X might not be
immediately (or ever) visible to thread Y:
- The compiler hoisted the variable into a register.
- The compiler optimized away the assignment entirely.
- The compiler reordered the write with other operations.
- The processor reordered the write with other operations.
- The value got written to a memory cache accessible to X but not Y
(such as a per-processor cache), and it has not yet been flushed to
where Y can see it.
- The value got written and flushed to where Y can see it, but Y
doesn't reload it, instead reading it from a cache holding a stale value.
As you can see from this list of reasons "weird things could happen",
speaking in terms of any one of these mechanisms is not likely to be
helpful except as a motivation for why worrying about memory visibility
is important. Beyond that, any understanding you have of the underlying
hardware is probably counterproductive. (In other words, the more you
know about cache coherency protocols, the more likely that "hardware
perspective" is likely to pollute your mental model and get in the way
of thinking in terms of the JMM.)
So instead, the new JMM defines a partial ordering, happens-before, and
a list of ways a HB ordering can be set up between a write and a read.
If there is a HB ordering between a write operation W and a read
operation R of that same variable, the reading thread is guaranteed to
see a value that is at least as up-to-date as the value written by W.
More information about the Concurrency-interest