0

In VC 2015, and recent versions of Clang and GCC (and according to the C++11 standard), initialisation of local static variables is thread-safe.

How efficient is the actual implementation of this in the above compilers? Do they use a mutex?

I'm thinking of writing a high performance logging library that uses a static object at each call site (like Ayxia Trace) - will that be slow?

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • Initialisation of a static variable happens _once_ for each run of the program, so issues of "efficiency" are not likely to be important. –  Feb 24 '17 at 17:29
  • It's actually compiler implementation specific. GCC and Clang are open source, you could check yourself what's used for that. – πάντα ῥεῖ Feb 24 '17 at 17:30
  • 3
    @NeilButterworth but checking if the initialisation has been done yet happens every time the function is called. It's not slow though, and if you need the functionality you won't be able to do something faster by hand. – Jonathan Wakely Feb 24 '17 at 17:31
  • @Jonatan Fair point. – πάντα ῥεῖ Feb 24 '17 at 17:31
  • _@Timmmm_ I don't really know what's actually used, but I believe it shouldn't be a mutex. Rather some `std::atomic` like thingy. – πάντα ῥεῖ Feb 24 '17 at 17:34
  • 2
    related: http://stackoverflow.com/questions/26013650/threadsafe-lazy-initialization-static-vs-stdcall-once-vs-double-checked-locki – NathanOliver Feb 24 '17 at 17:35
  • @Jon That check has to be done anyway, but it doesn't have to be thread safe, as it's a good candidate for double-checked locking. –  Feb 24 '17 at 17:35
  • @Timmmm, recent versions of GCC like 4.0, released 12 years ago. – Jonathan Wakely Feb 24 '17 at 17:36
  • @Neil _"but it doesn't have to be thread safe"_ According to the current standard it has to be thread safe. That's guaranteed. – πάντα ῥεῖ Feb 24 '17 at 17:37
  • @πάντα The initial check for initialisation doesn't need to be thread-safe - the initialisation itself does. –  Feb 24 '17 at 17:40
  • For GCC andClang http://mentorembedded.github.io/cxx-abi/abi.html#once-ctor describes the API (but not the implementation details) – Jonathan Wakely Feb 24 '17 at 17:41
  • @NeilButterworth Not thread safe, in the context of C++, means "undefined behavior if two threads try to do it". So yes, it has to be thread safe to check if initialization happened, but there are acceptable failure conditions. For example, false positives are ok, so long as the false positive leads to a second reliable check; false negatives are not ok. Such a system is still "thread safe", in that multiple threads behavior ends up in a set of defined possibilities, but not "thread safe" as in "behaves like non-threaded code" or what have you. – Yakk - Adam Nevraumont Feb 24 '17 at 18:32
  • @Yak Post a link to where in the standard it says "it has to be thread safe to check if initialization happened" –  Feb 24 '17 at 18:38
  • @NeilButterworth The behavior is defined. It may not format your hard drive, for example; If you violate thread safety in the context of C++ you permit undefined behavior; nasal demons, hard drive formatting, reading wrong value, reading a partly wrong value, caching the value before it was read, reading values whose state is inconsistent, or *anything*. Once the behavior is defined, it is "thead safe". – Yakk - Adam Nevraumont Feb 24 '17 at 18:44

0 Answers0