]> git.mxchange.org Git - simgear.git/commitdiff
Ensure sglog() singleton is threadsafe.
authorJames Turner <zakalawe@mac.com>
Wed, 4 Sep 2013 07:32:05 +0000 (08:32 +0100)
committerJames Turner <zakalawe@mac.com>
Wed, 4 Sep 2013 07:45:06 +0000 (08:45 +0100)
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

index 1c798d808558952a09e2bfc19106cb31f60bb8ea..36f2c70c7ced8da19369106f97b5316ee35090a4 100644 (file)
@@ -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<SGMutex> g(m);
+    
     if( !global_logstream )
         global_logstream = new logstream();
     return *global_logstream;