Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible or reasonable to implement the double-checked locking in Delphi?

As I known, there are two common kinds of practices to ensure the thread safety of lazy-initialization:

  1. Double-checked locking (Marks the variable as volatile to avoid the memory ordering)
  2. InterlockedCompareExchangePointer

It seems VCL uses the second practice. Is there any reason?

class function TEncoding.GetUTF8: TEncoding;
var
  LEncoding: TEncoding;
begin
  if FUTF8Encoding = nil then
  begin
    LEncoding := TUTF8Encoding.Create;
    if InterlockedCompareExchangePointer(Pointer(FUTF8Encoding), LEncoding, nil) <> nil then
      LEncoding.Free;
  end;
  Result := FUTF8Encoding;
end;

or is there any better method?

Thanks!

like image 960
Baoquan Zuo Avatar asked Apr 08 '10 12:04

Baoquan Zuo


People also ask

When is double checked locking not needed in C++?

For the singleton pattern, double-checked locking is not needed: If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. C++11 and beyond also provide a built-in double-checked locking pattern in the form of std::once_flag and std::call_once :

What is double checked locking in software engineering?

In software engineering, double-checked locking (also known as "double-checked locking optimization") is a software design pattern used to reduce the overhead of acquiring a lock by testing the locking criterion (the "lock hint") before acquiring the lock. Locking occurs only if the locking criterion check indicates...

Is the double-checked locking pattern (DCLP) safe?

The double-checked locking pattern (DCLP) is a bit of a notorious case study in lock-free programming. Up until 2004, there was no safe way to implement it in Java. Before C++11, there was no safe way to implement it in portable C++. The pattern gained attention for the shortcomings it exposed in those languages, and people began to write about it.

Does double-checked locking work in Java without synchronization?

Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment. Unfortunately, it will not work reliably in a platform independent way when implemented in Java, without additional synchronization.


1 Answers

There should not be much speed difference. In both approaches, global field is first checked if it is initialized and initialization is performed only when required. Therefore, most of the time the function will just do compare, jump, move, without any initialization.

When initialization is performed, InterlockedCompareEtc has two advantages over locking.

  1. It is faster.
  2. The code is shorter (no need to initialize a lock etc).

I find the InterlockedCompareEtc approach "neater" and use it in my code. But the locking would work equally well.

like image 177
gabr Avatar answered Oct 03 '22 04:10

gabr