1

I'v examine the assembly output in Windows of basic singleton implementation of using static variable to be initialized with new class instance from static function.

to my surprise, although the class has process-wide scope, the inner implementation uses access to TLS.

myClass::getInstance()
{
    static v = new myClass();
    return v; 
}

and the assembly output (the first relevant lines) :

push    rdi
sub     rsp, 40h
mov     rdi, rsp
mov     ecx, 10h
mov     eax, 0CCCCCCCCh
rep stosd
mov     [rsp+48h+var_18], 0FFFFFFFFFFFFFFFEh
mov     eax, 4
mov     eax, eax
mov     ecx, cs:_tls_index
mov     rdx, gs:58h
mov     rcx, [rdx+rcx*8]

Perhaps anybody can provide some insights about why this is needed (maybe performance) ?

thanks

1 Answer 1

1

It's generated by the thread-safe local static initialization feature.

Thread-safe static local variables use thread-local storage (TLS) internally to provide efficient execution when the static has already been initialized.

See here for the original proposal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.