Double-checked locking

When we are writing code that sometimes requires some object to be created, and sometimes not, we can save some execution time by only creating this object when it is needed. This process-lazy initialization is explained in Chapter 10, Singleton, Dependency Injection, Lazy Initialization, and Object Pool. That chapter, however, only covered single-threaded applications.

To do lazy initialization properly in a multithreaded world, we need locking. And to do it correctly and fast, we need a double-checked locking pattern. This pattern can speed up any lock that is only acquired if some condition is met. It is also called test, lock, and test again.

For instance, when you are changing lanes in a car, you check the traffic ...

Get Mastering Delphi Programming: A Complete Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.