Static constructor

SealabJaster sealabjaster at gmail.com
Thu Jan 14 18:40:04 UTC 2021


On Tuesday, 12 January 2021 at 11:28:12 UTC, ludo wrote:
> Ok, I agree that ends up being a kind of strange singleton. But 
> yes it was D v1 code. Do we agree that the following 
> multi-threaded singleton pattern is the proper way as of today.

It looks fine to me. The D wiki has the following example which 
prevents the need for entering the synchronized block more than 
once per thread: https://wiki.dlang.org/Low-Lock_Singleton_Pattern

I should note that the example doesn't specify the value as 
shared, which technically isn't the correct thing to do, but 
`shared` itself can be a bit of an annoyance. However, you do 
need to ensure that your static variable is either shared or 
__gshared, otherwise it becomes thread-local.

> If I understand, as of today an AA init depends on a runtime 
> function for historical reasons. A bit weird for an array 
> indeed perfectly known at compile time, but someday some  core 
> language contributor will have a look at it, I guess.

Maybe in 5 years' time we'll get an uneventful discussion on it 
where it'd take 2 years to come to a conclusion before another 
year for it to actually be implemented.

> If I understand well, this will accomplish the goal quoted with 
> no further comestics needed! Only one keyword over explicitely 
> using object mutex. Am I right?

I believe so. I've never used OpenAL so it may have additional 
restrictions with multithreading, but from a simple "This 
function is only ever executed on one thread at a time", your 
above suggestions should work.

Apologies for the late reply.



More information about the Digitalmars-d-learn mailing list