From: James Turner Date: Wed, 4 Sep 2013 07:32:05 +0000 (+0100) Subject: Ensure sglog() singleton is threadsafe. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dd93eb88f2cac2a2eb70c739f49efb8b829dbd67;p=simgear.git 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. --- 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;