Information Technology Reference
In-Depth Information
Memory barriers
Suppose you are writing low-level code that must reason about the ordering of mem-
ory operations. How can this be done on modern hardware and with modern compilers?
A memory barrier instruction prevents the compiler and hardware from reordering
memory accesses across the barrier—no accesses before the barrier will be moved
after the barrier and no accesses after the barrier will be moved before the barrier. One
can add memory barriers to the “too much milk” solution or to Peterson's algorithm to
get code that works on modern machines with modern compilers. Of course, that may
make such code even more complex.
Details of how to issue a memory barrier instruction depend on hardware and com-
piler details, but a good example is gcc's syncsynchronize() builtin, which tells the
compiler not to reorder memory accesses across the barrier and to issue architecture-
specific instructions that the underlying hardware will treat as a memory barrier.
The solution is complex, and it requires careful reasoning to convince
oneself that it works.
The solution is asymmetric. Thread A executes slightly different code than
Thread B. If we added more threads, more variations would be needed.
We should note that this limitation is not fundamental. For example,
Peterson dened a symmetric solution to a more general version of \Too
Much Milk" that works for any xed number n of threads attempting to
access a resource. More details on Petersen's algorithm can be found else-
where (e.g., http://en.wikipedia.org/wiki/Peterson's_algorithm . )
The solution is inecient. While thread A is waiting, it is \busy-waiting"|
consuming CPU resources.
The solution may fail if the compiler or hardware reorders instructions.
This limitation can be addressed through the use of memory barriers,
but the need to add memory barriers further increases the complexity of
trying to implement and reason about this type of algorithm; barriers also
do not address the other limitations just mentioned. See the sidebar for a
discussion of memory barriers.
5.1.5
A better solution
The next section will describe a better approach to writing programs in which
multiple threads access shared state. We will write shared objects that use
synchronization objects to coordinate dierent threads' access to shared state.
Search WWH ::




Custom Search