In C#, the following code (from this page) can be used to lazily instantiate a singleton class in a thread safe way:
class Foo {
private volatile Helper helper = null;
public Helper getHelper() {
if (helper == null) {
lock(this) {
if (helper == null)
helper = new Helper();
}
}
return helper;
}
}
What would be the equivalent thread safe Delphi code?
The article also mentions two problems with Double Checked Locking in Java:
So while the code of the C# and the Java version in the mentioned article look almost identical, only the C# version works as expected. Which leads to the additional question if these two problems also exist in a Delphi version of Double-Checked Locking?