C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Opt out of multi-threading protection for static variables inside functions

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Sun, 27 Oct 2024 20:36:16 +0000
On 27/10/2024 20:15, Lénárd Szolnoki via Std-Proposals wrote:
>
>
> On 27/10/2024 20:03, Thiago Macieira via Std-Proposals wrote:
>> On Sunday 27 October 2024 12:16:09 Pacific Daylight Time Lénárd
>> Szolnoki via
>> Std-Proposals wrote:
>>> It's possible to effectively opt-in. Have a static constinit
>>> optional, and
>>> do the initialisation dance yourself.
>>
>> Effectively, no, it isn't. That still registers the std::optional
>> destructor on
>> first use, so the guard variable and all the verification we've talked
>> about is
>> still present.
>
> Eh, right. You want to put static constinit optional into namespace or
> class scope, then do the initialization dance in your function.

Wrapped in a macro:

https://godbolt.org/z/ffeY3xYrq

I guess an attribute (or else) would be nice. I don't think there are
ABI implications. If one implementation ignores the attribute, then they
emit and use the guard variable, if an other doesn't ignore it then they
don't do any of that. This looks fine.

>
> I have no idea about the implications about destruction. I have a hunch
> that this work-around is acceptable most of the time when you don't care
> about thread-safe initialization of a particular static variable.
>
> Lénárd

Received on 2024-10-27 20:36:19