C++11 supports thread-safe initialization of statics

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)
This commit is contained in:
Guillaume Munch 2016-07-30 20:28:44 +01:00
parent 4d7b912ca1
commit 2fd2e65745
6 changed files with 12 additions and 13 deletions

View File

@ -223,7 +223,6 @@ ConverterCache::~ConverterCache()
} }
// FIXME THREAD
ConverterCache & ConverterCache::get() ConverterCache & ConverterCache::get()
{ {
// Now return the cache // Now return the cache

View 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 // 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 // new temporary directory, etc) every time, and then to destroy it. So maybe
// it's worth just keeping this one around. // it's worth just keeping this one around.
// FIXME THREAD
static TempFile tempfile("clipboard.internal"); static TempFile tempfile("clipboard.internal");
tempfile.setAutoRemove(false); tempfile.setAutoRemove(false);
static Buffer * staticbuffer = theBufferList().newInternalBuffer( // The initialization of staticbuffer is thread-safe. Using a lambda
tempfile.name().absFileName()); // guarantees that the properties are set only once.
static Buffer * staticbuffer = [&](){
// These two things only really need doing the first time. Buffer * b =
staticbuffer->setUnnamed(true); theBufferList().newInternalBuffer(tempfile.name().absFileName());
staticbuffer->inset().setBuffer(*staticbuffer); 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 // Use a clone for the complicated stuff so that we do not need to clean
// up in order to avoid a crash. // up in order to avoid a crash.
Buffer * buffer = staticbuffer->cloneBufferOnly(); Buffer * buffer = staticbuffer->cloneBufferOnly();

View File

@ -1251,7 +1251,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
stopBlinkingCursor(); stopBlinkingCursor();
// last_width : for checking if last preedit string was/wasn't empty. // 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? // We could have more than one work area, right?
static bool last_width = false; static bool last_width = false;
if (!last_width && preedit_string.empty()) { if (!last_width && preedit_string.empty()) {

View File

@ -44,7 +44,6 @@ public:
}; };
// FIXME THREAD
Cache & Cache::get() Cache & Cache::get()
{ {
// Now return the cache // Now return the cache

View File

@ -78,7 +78,6 @@ static int const s_numimages_ = 10;
static int const s_millisecs_ = 500; static int const s_millisecs_ = 500;
// FIXME THREAD
LoaderQueue & LoaderQueue::get() LoaderQueue & LoaderQueue::get()
{ {
static LoaderQueue singleton; static LoaderQueue singleton;

View File

@ -64,9 +64,9 @@ int timeout_min()
string const python(bool reset) string const python(bool reset)
{ {
// FIXME THREAD
// Check whether the first python in PATH is the right one. // Check whether the first python in PATH is the right one.
static string command = python23("python -tt"); static string command = python23("python -tt");
// FIXME THREAD
if (reset) { if (reset) {
command = python23("python -tt"); command = python23("python -tt");
} }