[libcxx-commits] [PATCH] D104987: Use GetSystemTimePreciseAsFileTime() if available

Adrian McCarthy via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 2 16:45:47 PDT 2021


amccarth added a comment.

In D104987#2856224 <https://reviews.llvm.org/D104987#2856224>, @mstorsjo wrote:

> In D104987#2855606 <https://reviews.llvm.org/D104987#2855606>, @amccarth wrote:
>
>> tl;dr:  I understand the desire to avoid even tiny synchronization costs when trying to read a high-precision timer, but I'm uncomfortable with the data race.  I suggest initializing the static function pointer under the locking provided by the compiler.
>>
>> The simplest option would be to initialize it where it's defined.  I would move the code that finds the address to a separate function, say `GetGetSysTimeProcAddress`, and then simplify this code to:
>>
>>   static GetSysTime_t getSysTime_p = GetGetSysTimeProcAddress();
>>   getSysTime_p(&ft);
>
> Oh, that's great, I had no idea that this actually ends up synchronized. That's very neat.

Yes, it was formalized in C++11 and is sometimes called "magic statics."  I believe Clang and GCC may have implemented them even earlier.  MSVC did not implement them until VS 2015.

Back in 2004, Raymond Chen wrote a blog post <https://devblogs.microsoft.com/oldnewthing/20040308-00/?p=40363> suggesting that _not_ synchronizing function static initialization was intentional and appropriate.

MSVC has a command line switch to disable the injection of the synchronization code for old programs that still expect the old behavior.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104987/new/

https://reviews.llvm.org/D104987



More information about the libcxx-commits mailing list