Liste des Groupes | Revenir à cl c++ |
An example of pre-C++11 thread-safe Meyer singleton:That's rather slow. Double-checked locking as implemented for
std::map<std::string, std::string>& GetGlobalMap() {
// No dynamic initialization, so this is safe:
static std::map<std::string, std::string>* pGlobal = NULL;
// No dynamic initialization, so this is safe as well:
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// Initialize the global static if not yet initialized.
pthread_mutex_lock(&mutex);
if (!pGlobal) {
pGlobal = new std::map<std::string, std::string>();
}
pthread_mutex_unlock(&mutex);
return *pGlobal;
}
Les messages affichés proviennent d'usenet.