]> git.lyx.org Git - lyx.git/commitdiff
C++11 supports thread-safe initialization of statics
authorGuillaume Munch <gm@lyx.org>
Sat, 30 Jul 2016 19:28:44 +0000 (20:28 +0100)
committerGuillaume Munch <gm@lyx.org>
Sun, 31 Jul 2016 17:34:33 +0000 (18:34 +0100)
A static local variable is guaranteed to be initialized only once, and in time.

Lambda expressions can be used to perform complex initialization of those static
variables on the spot.

(starting from: gcc >= 4.8, msvc >= 2015)

src/ConverterCache.cpp
src/CutAndPaste.cpp
src/frontends/qt4/GuiWorkArea.cpp
src/graphics/GraphicsCache.cpp
src/graphics/GraphicsLoader.cpp
src/support/os.cpp

index 29d07e4a04a5b7ce05aca502cc1c4d17b712d7f9..b87bfe56ccdfc8df7e1b222c7fc830749e272a34 100644 (file)
@@ -223,7 +223,6 @@ ConverterCache::~ConverterCache()
 }
 
 
-// FIXME THREAD
 ConverterCache & ConverterCache::get()
 {
        // Now return the cache
index 988dba41542fd375439dc54f5a6d5f2b175302c8..4ec9811d90bb8fd086e2e30be20ecb958bf1b620 100644 (file)
@@ -486,16 +486,18 @@ Buffer * copyToTempBuffer(ParagraphList const & paragraphs, DocumentClassConstPt
        // to be so, but the alternative is to construct a new one of these (with a
        // new temporary directory, etc) every time, and then to destroy it. So maybe
        // it's worth just keeping this one around.
-       // FIXME THREAD
        static TempFile tempfile("clipboard.internal");
        tempfile.setAutoRemove(false);
-       static Buffer * staticbuffer = theBufferList().newInternalBuffer(
-                       tempfile.name().absFileName());
-
-       // These two things only really need doing the first time.
-       staticbuffer->setUnnamed(true);
-       staticbuffer->inset().setBuffer(*staticbuffer);
-
+       // The initialization of staticbuffer is thread-safe. Using a lambda
+       // guarantees that the properties are set only once.
+       static Buffer * staticbuffer = [&](){
+               Buffer * b =
+                       theBufferList().newInternalBuffer(tempfile.name().absFileName());
+               b->setUnnamed(true);
+               b->inset().setBuffer(*b);
+               //initialize staticbuffer with b
+               return b;
+       }();
        // Use a clone for the complicated stuff so that we do not need to clean
        // up in order to avoid a crash.
        Buffer * buffer = staticbuffer->cloneBufferOnly();
index 75afebfd02e1e69750cba20c497cb0d37331631f..69090b32921e4021f83f31adff9976feab2bc66d 100644 (file)
@@ -1251,7 +1251,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
                stopBlinkingCursor();
 
        // last_width : for checking if last preedit string was/wasn't empty.
-       // FIXME THREAD
+       // FIXME THREAD && FIXME
        // We could have more than one work area, right?
        static bool last_width = false;
        if (!last_width && preedit_string.empty()) {
index 5ed905252d71343d9571aea74b975eb2c4d88e3b..a51f2322fd6b112bcab0f68f33b8c9a0ea62cebd 100644 (file)
@@ -44,7 +44,6 @@ public:
 };
 
 
-// FIXME THREAD
 Cache & Cache::get()
 {
        // Now return the cache
index 1fa5c65019a7fe687a9cc4024eb1cf1e1b8c6896..36a6c06d2c12748f2f834612d756c313e984b55d 100644 (file)
@@ -78,7 +78,6 @@ static int const s_numimages_ = 10;
 static int const s_millisecs_ = 500;
 
 
-// FIXME THREAD
 LoaderQueue & LoaderQueue::get()
 {
        static LoaderQueue singleton;
index c21d9c5d3a39f80e03d3f0c938dae15c8b1de9d6..9dc19e92897389caa70f38b4476afbfc161c1061 100644 (file)
@@ -64,9 +64,9 @@ int timeout_min()
 
 string const python(bool reset)
 {
-       // FIXME THREAD
        // Check whether the first python in PATH is the right one.
        static string command = python23("python -tt");
+       // FIXME THREAD
        if (reset) {
                command = python23("python -tt");
        }