I have some objects defined as global, which means their constructor are entered before program start or under DllMain(). This causes some issues, and that is why I want to move them to local static.
Our main project are mainly under Visual Studio 2012. According to the these post.
- “Magic static” singleton crashing when referenced in static destruction phase of another translation unit"
- Support For C++11/14/17 Features (Modern C++)
The Magic Statics is not implemented in Visual Studio 2012 (until VS 2015). So the local static variable initialization is not automatically protected from concurrent access by compiler.
What is a suitable workaround or solution for this case?
Here is what I tried:
Add a lock to protect. But if the variable initialized before the function is entered, my lock may be useless...
// data object
struct Cat
{
Cat()
{
std::cout << __FUNCTION__ << "\n";
}
};
// lock mutex for singleton getter
static std::mutex getcat_m;
// singleton getter
Cat& GetCat(){
std::cout << __FUNCTION__ << " In\n";
std::lock_guard<std::mutex> lk(getcat_m);
static Cat cat;
std::cout << __FUNCTION__ << " Out\n";
return cat;
}
int main(int argc, char* argv[])
{
std::cout << __FUNCTION__ << " In\n";
Cat& cat = GetCat();
std::cout << __FUNCTION__ << " Out\n";
std::cin.ignore();
return 0;
}
It actually shows as expected. The constructor is called after function is entered.
main In
GetCat In
Cat::Cat
GetCat Out
main Out
But I am not sure whether this is a suitable solution.