Warning! Threading in a multiprocessor world
Find out why many tricks to avoid synchronization overhead just don't work
By Allen Holub, JavaWorld.com, 02/09/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
I had intended this month to carry on with presenting a caching class loader -- that was the whole point of the zip-file access
classes I presented in the last couple of columns. I've decided to digress for this month, however, on a threading-related
issue that's important enough that I wanted to bring it to your attention sooner rather than later.
Double-checked locking can be hazardous to your code!
This week
JavaWorld focuses on the dangers of the double-checked locking idiom. Read more about how this seemingly harmless shortcut can wreak
havoc on your code:
This article discusses why a whole category of techniques that programmers misguidedly use to avoid synchronization overhead
simply don't work. Probably the most common of those is double-checked locking, which I'll discuss in a moment. In a recent posting to a Java Memory Model mailing list, Bill Pugh at the University of Maryland
(one of the folks who's spearheading the rewriting of the JLS (Java Language Specification) to compensate for some of the
problems discussed this month) said, "...this double-check idiom is like a roach infestation. It doesn't matter how many of
them you crush under your shoe, more of them crawl out from under the refrigerator." Well, I hope this article will help stamp
out the swarm.
So what's the problem?
In the beginning, a CPU could write directly to memory simply by wiggling the voltages on a few wires that connected the CPU
chip to the memory chip. All was well with the world. Even with multithreaded systems, only one path to memory existed, and
reads and writes to memory always occurred whenever the CPU executed the associated machine instruction. The introduction
of memory caches didn't fundamentally change that model (once they got the cache-coherency bugs worked out). Indeed, the cache
is transparent to the program if it's implemented correctly. That simple memory model -- the CPU issues an instruction that
modifies memory with an immediate effect -- remains in most programmers' minds.
Then somebody had the bright idea that two processors could run in the same box at the same time, sharing a common memory
store (Suddenly, the world became much more complicated). In that situation, a given CPU can no longer access memory directly
because another CPU might be using the memory at the same time. To solve the problem, along came a traffic-cop chip, called
a memory unit. Each CPU was paired with its own memory unit, and the various memory units coordinated with each other to safely access the
shared memory. Under that model, a CPU doesn't write directly to memory but requests a read or write operation from its paired
memory unit, which updates the main memory store when it can get access, as seen in Figure 1. Those early memory units effectively
managed simple read or write request queues.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- In "Double-checked lockingClever, but broken" (JavaWorld, February 9, 2001), Brian Goetz looks at the problem from another angle
http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html
- Find all of Allen Holub's Java Toolbox columns in JavaWorld: http://www.javaworld.com/javaworld/topicalindex/jw-ti-toolbox.html
- You'll find numerous Singleton-related do's and don'ts, including the double-checked locking idiom, in "When is a Singleton
not a Singleton," Joshua Fox (JavaWorld, January 2001)
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.html
- For answers to your pressing design patterns questions, check out JavaWorld's Programming Theory & Practice discussion
http://www.itworld.com/jump/jw-0209-toolbox/forums.itworld.com/webx?14@@.ee6b806
- To quickly search for other important JavaWorld articles, listed by subject, visit our useful Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-index.html
- Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe
- Java memory-model issues in general are discussed on a Webpage that Bill Pugh at the University of Maryland set up expressly
for that purpose
http://www.cs.umd.edu/~pugh/java/memoryModel/
- Paul Jakubik at ObjectSpace has written a really nice article on problems of relaxed memory models called "Another Look at
Multiprocessor Safety in Java." Paul covers much of the same material as I have in the article, though more rigorously (for
those of you who like rigor). Find it at
http://www.primenet.com/~jakubik/mpsafe/MultithreadSafety.pdf
- Doug Lea has a very accurate, but somewhat hard to read, description of the Java memory model in his book Concurrent Programming in Java, 2nd ed. (Addison-Wesley, November 1999)
http://www.amazon.com/exec/obidos/ASIN/0201310090/javaworld
- Finally, an article by David Bacon (IBM Research), Joshua Bloch (Sun), Jeff Bogda, Cliff Click (Hotspot JVM project), Paul
Haahr, Doug Lea, Tom May, Jan-Willem Maessen, John D. Mitchell (jGuru), Kelvin Nilsen, and Bill Pugh, entitled "The Double-Checked
Locking is Broken Declaration," focuses explicitly on the problems of double-checked locking
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html