Visual Studio 2015编译器是否插入双重检查锁定?
我想让我的Singleton(GOF)模式线程安全(无锁)。
Singleton& Singleton::getInstance() {
static Singleton instance;
return instance;
}
是否可以生成汇编代码并检查?
答案 0 :(得分:2)
您可以在Debug-> Windows-> Disassembly。
中访问反汇编上课:
class S
{
public:
static S& getInstance()
{
static S instance;
return instance;
}
};
你得到反汇编:
47:
48: class S
49: {
50: public:
51: static S& getInstance()
52: {
push ebp
mov ebp,esp
sub esp,0C0h
push ebx
push esi
push edi
lea edi,[ebp-0C0h]
mov ecx,30h
mov eax,0CCCCCCCCh
rep stos dword ptr es:[edi]
53: static S instance;
54: return instance;
mov eax,offset instance (0D9471h)
55: }
pop edi
pop esi
pop ebx
mov esp,ebp
pop ebp
ret