[cpp-threads] static locals

Alexander Terekhov alexander.terekhov at gmail.com
Tue May 31 13:51:41 BST 2005


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);

regards,
alexander.




More information about the cpp-threads mailing list