C++ Static Initializer - Is it thread safe
- by Yan Cheng CHEOK
Usually, when I try to initialize a static variable
class Test2 {
public:
static vector<string> stringList;
private:
static bool __init;
static bool init() {
stringList.push_back("string1");
stringList.push_back("string2");
stringList.push_back("string3");
return true;
}
};
// Implement
vector<string> Test2::stringList;
bool Test2::__init = Test2::init();
Is the following code thread safe, during static variable initialization?
Is there any better way to static initialize stringlist, instead of using a seperate static function (init)?