Summary
Let's conclude this chapter on locked data structures with a summary.
You were introduced to a sampling of concurrent data structures, from counters to lists and queues, and finally to the ubiquitous and heavily-used hash table. You learned a few important lessons along the way:
- To be careful with the acquisition and release of locks around control flow changes.
- Enabling more concurrency does not necessarily increase performance.
- Performance problems should only be remedied once they exist.
This last point, of avoiding premature optimization, is central to any performance-minded developer; there is no value in making something faster if doing so will not improve the overall performance of the application.
Of course, we have just scratched the surface of high-performance structures.
TIP: AVOID PREMATURE OPTIMIZATION (KNUTH’S LAW)
When building a concurrent data structure, start with the most basic approach, which is to add a single big lock to provide synchronized access. By doing so, you are likely to build a correct lock; if you then find that it suffers from performance problems, you can refine it, thus only making it fast if need be. As Knuth famously stated, “Premature optimization is the root of all evil.”
Many operating systems utilized a single lock when first transitioning to multiprocessors, including Sun OS and Linux. In the latter, this lock even had a name, the big kernel lock (BKL). For many years, this simple approach was a good one, but when multi-CPU systems became the norm, only allowing a single active thread in the kernel at a time became a performance bottleneck. Thus, it was finally time to add the optimization of improved concurrency to these systems. Within Linux, the more straightforward approach was taken: replace one lock with many. Within Sun, a more radical decision was made: build a brand new operating system, known as Solaris, that incorporates concurrency more fundamentally from day one. Read the
and Linux “Understanding the Linux Kernel (Third Edition)” by Daniel P. Bovet and Marco Cesati. O’Reilly Media, November 2005. The classic book on the Linux kernel. You should read it. kernel books for more information about these fascinating systems. Solaris “Solaris Internals: Core Kernel Architecture” by Jim Mauro and Richard McDougall. Prentice Hall, October 2000. The Solaris book. You should also read this, if you want to learn about something other than Linux.
Get hands-on with 1400+ tech skills courses.