Home arrow C++ arrow Page 5 - C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe

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.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
August 01, 2005
  1. · C++ in Theory: Why the Double Check Lock Pattern Isn`t 100% Thread Safe
  2. · The Original Problem
  3. · The Double Checked Locking Pattern
  4. · Object Creation
  5. · Multiprocessor Machines
  6. · Portable Solutions

print 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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials