C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe
Back in January, Jun Nakamura discussed Singletons in a series of three articles, and revisited the subject in May. In the May article, he described the Double Checked Locking Pattern as a way to make Singletons thread safe. Unfortunately, it's not that simple, as he explains in this article.
C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe - Multiprocessor Machines (Page 5 of 6 )
So should we worry about multithreading? There is a switch for the compiler that forces it to generate single threaded code: we’ll get out of jail free, pass go and collect 200 bucks, right?
Maybe you are safe for now, but if you have followed the chip designs of Intel lately (and heard about that massive parallel CELL chip that is going to power the Playstation 3), by the end of 2005 it will be perfectly normal for home PCs to have a Dual Core CPU (a CPU effectively containing two physical processors), where both cores are hyperthreaded as well. A hyperthread is a nifty way of maximizing instruction ordering on the CPU by filling up time that would normally remain unused. This makes it act like two logical processors while you are physically only running one. Thus PCs will appear to have four logical processors on board! (For dual processor machines the number of logical processors ramps up to eight!)
Still, if you are writing and compiling single threaded code, your application will only be able to run at 25 percent of its maximum speed. Why? Because the Task Manager will only show that the processor is running at 100% when all four hyperthreads are being maxed out.
It's a sneaky way to try and maintain Moore’s Law, but people will think your code is just plain slow! Concurrent programming is going to be an issue, and maybe sooner than you thought.