How to correctly implement the Singleton pattern in C++ with multithreading in mind?
Hello everyone! I'm a beginner C++ developer and trying to understand the Singleton pattern. I've read that it's a creational pattern that guarantees a class has only one instance and provides a global access point to it.
I wrote a simple implementation for a logger:
```cpp
class Logger {
public:
static Logger* getInstance() {
if (instance == nullptr) {
instance = new Logger();
}
return instance;
}
void log(const std::string& msg) {
std::cout << msg << std::endl;
}
private:
Logger() {}
static Logger* instance;
};
Logger* Logger::instance = nullptr;
```
But several points bother me:
1. In a multithreaded environment, multiple instances could be created if two threads simultane