4 ms·
I would be wary of copying this code and using it in my own project. Generally speaking, it's best to avoid rolling your own thread primitives. Even the smartes
by KerrAvon 3y ago
I would be wary of copying this code and using it in my own project. Generally speaking, it's best to avoid rolling your own thread primitives. Even the smartest professionals sometimes get it subtly wrong (and sometimes it only becomes clear at scale via analytics and studying the data, luxuries you probably don't have). Use the battle-tested primitives: pthread_once, whatever the C++ equivalent is, or dispatch_once on the Mac.
- cjensen 3y agoThe C++ equivalent is... it just works. Static variables in a function are initialized exactly once the first time a function is entered per the language definition. This is one of the many great improvements of C++11
- variadix 3y agoExcept you pay this cost (locks + codegen) even for PODs with known data at compile time.
- cjensen 3y agoNo. PODs that are initialized with no side-effects can be optimized away into compile-time initialization. E.g. if you have an integer that is static inside a function, it can just be initialized old-school.
- memefrog 3y agoWhat a pointless comment. If you are doing anything with threads it is far more likely you have a bug in the way you use mutexes than that there is a bug in this code.