싱글 톤이 스레드에 안전하지 않다고 읽었습니다. 나는 이것이 왜 있는지 이해하려고 노력하고 있습니다. 나는이 같은 단일 개체가있는 경우 :싱글 톤 스레드가 안전하지 않은 이유는 무엇입니까?
class singleton final
{
public:
static singleton& instance()
{
static singleton unique;
return unique;
}
private:
singleton() = default;
singleton(singleton const&) = delete;
singleton& operator=(singleton const&) = delete;
};
을 그리고 난 같은 코드가있는 경우 :
singleton *p1, *p2;
auto t1 = std::thread([] { p1 = &singleton::instance(); });
auto t2 = std::thread([] { p2 = &singleton::instance(); });
t1.join();
t2.join();
그것이 p1
및 p2
가능한가 두 개의 서로 다른 singleton
인스턴스를 가리 키도록? unique
이 static
인 경우 "정적"특성이 완전히 초기화 될 때까지 영향을 미치지 않습니까? 그렇다면 정적 객체의 초기화를 동시에 액세스 할 수 있으므로 여러 정적 객체를 만들 수 있습니까? 접두사 이름 컴파일러가있다 -은 "__"이
bool __instance_initialized = false;
alignas(X) char __buf_instance[sizeof(X)];
// ...
X& instance()
{
if (!__instance_initialized)
{
::new(__buf_instance) X;
__instance_initialized = true;
}
return *static_cast<X*>(__buf_instance);
}
:
X& instance()
{
static X x;
return x;
}
이 코드가 같은 것을 할 것이라는 점을 의미한다 : C++ 98/03 파일 현지 정적에서
정적 초기화 규칙이 C++ 11에서 스레드 안전성이 변경되었습니다. – Jarod42
@ Jarod42 알아 두지 만 왜 스레드로부터 안전하지 않은지 이해하고 싶습니다. –
아직 하나의 객체가 있지만 초기화가 스레드 안전하지 않습니다. 초기화되지 않은 객체를 반환하는 것은 발생할 수 있습니다 (이중 초기화는 확실하지 않습니다) – Jarod42