[cpp-threads] static locals

Peter Dimov pdimov at mmltd.net
Tue May 31 15:21:39 BST 2005


Alexander Terekhov wrote:
> On 5/30/05, Peter Dimov <pdimov at mmltd.net> wrote:
> [...]
>>>> 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.
>
> That's not enough. Dynamic initialization may "publish" something
> that was written prior to it. Suppose that "constructor of Q" will
> publish address of some (thread local) string with a name of the
> creator thread and that string object is initialized prior to Q
> instance. Or just...
>
>>
>> IOW:
>>
>> void f()
>
> int f(int & g)
>
>> {
>>    static int i = g();
>
>    static(synchronized) int & i = g;
>    return i;
>
>> }
>>
>> // thread 1
>>
>> x = 5;
>> f();
>
> a = f(x);
>
>>
>> // thread 2
>
> y = -5;
> b = f(y);
>
> // initial thread
>
> t1 = new_thread(thread_one);
> t2 = new_thread(thread_two);
>
> t1->join();
> t2->join();
>
> assert(a == b);

Yes, you are right. I thought about "all side effects and all dependencies 
should be visible" but this is too complicated and probably doesn't buy 
anything. So all prior writes it is (but only in the dynamic initialization 
case, of course). 





More information about the cpp-threads mailing list