[cpp-threads] static locals

Peter Dimov pdimov at mmltd.net
Mon May 30 22:14:23 BST 2005


Alexander Terekhov wrote:
> On 5/30/05, Peter Dimov <pdimov at mmltd.net> wrote:
> [...]
>> void f()
>> {
>>    static Q* p = new Q( ... );
>
> static(synchronized) or something to distinguish it from
> threads-unaware lazi-init -- just suppose that "new Q( ... )" is
> "lock-free" (but not necessarily "thread-safe"). Why the heck do you
> want to penalize all users of "classic" local statics with IA64-like
> lazy-init guards and ld.acq on fast path (TSD/TLS variation of DCSI
> aside for a moment)?

Because...

1. Solutions that do not need syntactic language changes have better chances 
to be accepted;

2. It is better to be safe by default than fast but potentially unsafe by 
default;

3. I can get the unsafe behavior if I want to by using a static bool;

and

4. It is possible to optimize the thread-safe init quite well, in principle, 
and compiler writers are much more likely to invest in such optimizations if 
the default is thread-safe.

> Uhmm. See also
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20099
> http://groups.google.de/group/comp.programming.threads/msg/020cc14bc04e8983
> http://groups.google.de/group/comp.programming.threads/msg/b143891d5f713b7f

I will, later.

>> the writes done by the constructor of Q should be visible to all
>> threads that call f.
>
> And prior writes too (in absence of subsequent data races).

I think that prior writes should not be required to be visible. Or to be 
more precise, I think that all side effects of the dynamic initialization 
should be visible, but no more than that.

IOW:

void f()
{
    static int i = g();
}

// thread 1

x = 5;
f();

// thread 2

f(); // x may or may not be 5 here, but all side effects of g() are visible





More information about the cpp-threads mailing list