| Deutsch English Français Italiano |
|
<vse1lm$8iai$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Paavo Helde <eesnimi@osa.pri.ee>
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: <vse1lm$8iai$1@dont-email.me>
References: <vsb12i$2mv42$1@dont-email.me> <vsbl21$1jsvi$1@dont-email.me>
<vsbo2p$1p5ov$1@dont-email.me> <dTdGP.136957$cgs7.71710@fx14.ams4>
<vsbqc2$1rk0n$1@dont-email.me> <48eGP.730204$J61.500281@fx08.ams4>
<vsbr65$1sgoe$1@dont-email.me> <YgeGP.136960$cgs7.11888@fx14.ams4>
<vsdkpe$3r9qf$1@raubtier-asyl.eternal-september.org>
<vsdlc3$3rhjh$2@dont-email.me>
<vsdm8c$3sq3u$1@raubtier-asyl.eternal-september.org>
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: <vsdm8c$3sq3u$1@raubtier-asyl.eternal-september.org>
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<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;
}