Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Paavo Helde Newsgroups: comp.lang.c++ Subject: Re: Pre-main construction order in modules Date: Mon, 31 Mar 2025 15:26:30 +0300 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: <48eGP.730204$J61.500281@fx08.ams4> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 31 Mar 2025 14:26:31 +0200 (CEST) Injection-Info: dont-email.me; posting-host="8c6b456c1efc908c50b1622f28b23438"; logging-data="280914"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18x0QCy6RKEuy6IBvUk8C9M/AtF/isxLik=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:45egyL5hX8GdlloZ2efo2fuVzs4= In-Reply-To: Content-Language: en-US Bytes: 2748 On 31.03.2025 12:11, Bonita Montero wrote: > Am 31.03.2025 um 10:56 schrieb Paavo Helde: > >> Before C++11 one had to just add their own synchronization for thread >> safety (assuming multi-threaded access was needed, which was not so >> often in the past). >> C++11 just made coding of a Meyer's singleton easier, that's all. > > That's not that easy since it is undefined when a static local variable > is initialized before C++11. You'd have to wrap it in a union and leave > only member which is the object uninitialized. That's possible, but > that's not Meyer's singleton. Thread synchronization has been possible since the introduction of threads. Otherwise they would not have been usable. An example of pre-C++11 thread-safe Meyer singleton: std::map& GetGlobalMap() { // No dynamic initialization, so this is safe: static std::map* 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(); } pthread_mutex_unlock(&mutex); return *pGlobal; }