samedi 14 mai 2022

Defining an atomic<> as a static variable inside a function

C++11 mandates that static variables inside a function are atomically initialized for the first run of the function. The only way to do that efficiently is double-checked locking to prevent an expensive mutex-lock all the time.
I just had the necessity to have an static atomic<size_t> inside a function. It wouldn't make sense if such an atomic would be atomically initialized through double-checked locking. Instead it should be handled like another static initialized non-const native variable.
I checked the generated code with VC++ and Intel C++ under Windows and clang++ and g++ under Linux and all generated code according to my expectations. But is this really reliable and mandated by the standard that atomics should be handled independent of other objects here ?

Aucun commentaire:

Enregistrer un commentaire