From dd93eb88f2cac2a2eb70c739f49efb8b829dbd67 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 4 Sep 2013 08:32:05 +0100 Subject: [PATCH] Ensure sglog() singleton is threadsafe. Slightly simplistic, but definitiely correct, method for ensuring singleton creation is threadsafe. Can be optimised if lock contention is ever an issue. --- simgear/debug/logstream.cxx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/simgear/debug/logstream.cxx b/simgear/debug/logstream.cxx index 1c798d80..36f2c70c 100644 --- a/simgear/debug/logstream.cxx +++ b/simgear/debug/logstream.cxx @@ -348,6 +348,13 @@ sglog() { // Force initialization of cerr. static std::ios_base::Init initializer; + + // http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf + // in the absence of portable memory barrier ops in Simgear, + // let's keep this correct & safe + static SGMutex m; + SGGuard g(m); + if( !global_logstream ) global_logstream = new logstream(); return *global_logstream; -- 2.39.5