Above VS 2015, with /Zc:threadSafeInit, the initialization of static local variables is thread-safe, but MSDN said
Thread-safe static local variables use thread-local storage (TLS) internally to provide efficient execution when the static has already been initialized.
Windows XP ignores the .tls segment if dynamically loading a Dll with LoadLibrary
, so
- Why use TLS and how does it work efficient ?
- Does std::call_once use TLS ?
- How to implement thread-safe-init without TLS?
The link of the citation is here Thread-safe Local Static Initialization
The implementation of this feature relies on Windows operating system support functions in Windows Vista and later operating systems. Windows XP, Windows Server 2003, and older operating systems do not have this support, so they do not get the efficiency advantage.
My test code:
class AA
{
public:
int m_a = 1;
};
AA* getAA()
{
static AA a;
return &a;
}
int main()
{
AA* pa = getAA();
return 0;
}
With /Zc:threadSafeInit
, function getAA
in windbg disassembles to:
008c1000 55 push ebp
008c1001 8bec mov ebp,esp
008c1003 64a12c000000 mov eax,dword ptr fs:[0000002Ch]
008c1009 8b08 mov ecx,dword ptr [eax]
008c100b 8b15b8338c00 mov edx,dword ptr [testStatic!__favor+0x4 (008c33b8)]
008c1011 3b9104000000 cmp edx,dword ptr [ecx+4]
008c1017 7e2d jle testStatic!getAA+0x46 (008c1046)
008c1019 68b8338c00 push offset testStatic!__favor+0x4 (008c33b8)
008c101e e809020000 call testStatic!_Init_thread_header (008c122c)
008c1023 83c404 add esp,4
008c1026 833db8338c00ff cmp dword ptr [testStatic!__favor+0x4 (008c33b8)],0FFFFFFFFh
008c102d 7517 jne testStatic!getAA+0x46 (008c1046)
008c102f b9bc338c00 mov ecx,offset testStatic!a (008c33bc)
008c1034 e817000000 call testStatic!AA::AA (008c1050)
008c1039 68b8338c00 push offset testStatic!__favor+0x4 (008c33b8)
008c103e e89f010000 call testStatic!_Init_thread_footer (008c11e2)
008c1043 83c404 add esp,4
008c1046 b8bc338c00 mov eax,offset testStatic!a (008c33bc)
008c104b 5d pop ebp
008c104c c3 ret
and with /Zc:threadSafeInit-
(close the feature), function getAA
disassembles to:
010e1000 55 push ebp
010e1001 8bec mov ebp,esp
010e1003 a180330e01 mov eax,dword ptr [testStatic!a+0x4 (010e3380)]
010e1008 83e001 and eax,1
010e100b 7519 jne testStatic!getAA+0x26 (010e1026)
010e100d 8b0d80330e01 mov ecx,dword ptr [testStatic!a+0x4 (010e3380)]
010e1013 83c901 or ecx,1
010e1016 890d80330e01 mov dword ptr [testStatic!a+0x4 (010e3380)],ecx
010e101c b97c330e01 mov ecx,offset testStatic!a (010e337c)
010e1021 e80a000000 call testStatic!AA::AA (010e1030)
010e1026 b87c330e01 mov eax,offset testStatic!a (010e337c)
010e102b 5d pop ebp
010e102c c3 ret
why use TLS
is more question to Microsoft support than to public. The same is aboutstd::call_once
implementation by Microsoft. However, you should have no issues about that. I created a lot of software with Visual C++ and I never had any observable performance issues due to static initialization orcall_once
. If you need something very specific, you can use low level OS API to implement your static initialization yourself, but I am sure you don't really need it./Zc:threadSafeInit-
is useful, but static local variable is not threadsafe with this compiler option.