Linked Questions
15 questions linked to/from Are function static variables thread-safe in GCC?
251 votes
2 answers
100k views
Is local static variable initialization thread-safe in C++11? [duplicate]
I know this is an often asked question, but as there are so many variants, I'd like to re-state it, and hopefully have an answer reflecting the current state. Something like Logger& g_logger() { ...
59 votes
4 answers
47k views
What makes a static variable initialize only once?
I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to ...
20 votes
8 answers
7k views
Thread-safe initialization of function-local static const objects
This question made me question a practice I had been following for years. For thread-safe initialization of function-local static const objects I protect the actual construction of the object, but ...
7 votes
6 answers
10k views
Managing a singleton destructor
The following small example implements a singleton pattern that I've seen many times: #include <iostream> class SingletonTest { private: SingletonTest() {} static SingletonTest *instance; ...
4 votes
6 answers
3k views
How to write code to be executed before main() gets control?
I am porting a class to C++ and need to execute some initialization code before the first instance of my class is created; executing the code before main() gets control suits me. How to do it in C++?
6 votes
4 answers
2k views
Is this way of creating static instance thread safe?
I have the following sample C++ code: class Factory { public: static Factory& createInstance() { static Factory fac; return fac; } private: Factory() { ...
9 votes
5 answers
210 views
Is there an issue with this singleton implementation?
I've typically gotten used to implementing a singleton pattern in this manner because it's so easy: class MyClass { public: MyClass* GetInstance() { static MyClass ...
4 votes
4 answers
3k views
Is it better to use `static const std::string` or just `const std::string` in a method/function?
I have a method / function: void foo() { static const std::string strSQLQuery = "SELECT ... "; // or maybe const std::string strSQLQuery = "SELECT ... "; // some operations on strSQLQuery ...
6 votes
4 answers
761 views
How to address thread-safety of service data used for maintaining static local variables in C++?
Consider the following scenario. We have a C++ function with a static local variable: void function() { static int variable = obtain(); //blahblablah } the function needs to be called from ...
4 votes
2 answers
1k views
Local static initialization without holding a lock avoids a possible deadlock in C++11?
In paper http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm an algorithm is presented that does not need to hold a lock during the initialization of a local static variable but still ...
8 votes
1 answer
2k views
GCC's TSAN reports a data race with a thread safe static local
I wrote the following toy example: std::map<char, size_t> getMap(const std::string& s) { std::map<char, size_t> map; size_t i = 0; for (const char * b = s.data(), *end = b ...
1 vote
2 answers
441 views
How to get ref from GetInstance from main app?
I'm following this singleton pattern, why error LNK2001: unresolved external symbol in this case? my problem LOOKs similar, but my issue is not with the definition of the static instance. My problem ...
0 votes
3 answers
627 views
C++ Threadsafe Singleton (NOT FOR INIT)
So I want to access a singleton class from multiple threads. Conceptually I'd think that calling non-const methods on this singleton instance would be not thread-safe. I've been looking online and no ...
-1 votes
1 answer
489 views
Websocket key hashing
I have a simple function to handle the parsing and hashing of the Sec-WebSocket-Key value in a C websocket program. I had the whole program working but found out I had a bunch of char* without a ...
0 votes
2 answers
234 views
Is a static variable definition inside a function thread safe
In C++, static variables defined inside a function are thread safe by default. So, are static variables in Rust have the same thread safety property or I need to use some sync primitive?