Liste des Groupes | Revenir à cl c++ |
Am 31.03.2025 um 14:26 schrieb Paavo Helde:Sure. That's why one should not call such a function from inside a million iteration loop. But it would often be possible to call it before the loop and use the returned reference million times inside the loop.
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;
}
all static locals with current runtimes is much more efficient.
Les messages affichés proviennent d'usenet.