[cpp-threads] static locals

Boehm, Hans hans.boehm at hp.com
Thu Jun 2 01:01:27 BST 2005


I think we agree.  The compiler is unlikely to perform the
initialization
statically, but when writing this code, it's hard for the programmer
to preclude that possibility.  If we were to go this route, we'd
have to add something to 3.6.2/2, (e.g. a requirement that the
initializer
return the same value in all legal executions), which probably wouldn't
be
a huge deal.  I doubt it would break any existing compilers.

Hans

> From: Peter Dimov [mailto:pdimov at mmltd.net] 
> To reply to Hans Boehm's post:
> 
> > Looking at this again, I agree that this is less clear than 
> I thought, 
> > and we could probably fix things to require thread-safety.
> >
> > But consider something like
> >
> > void f(int my_thread_id) {
> >  thread_info[my_thread_id] = ...;
> >  static int master_thread_id = my_thread_id;
> >
> >  ...thread_info[master_thread_id]...
> >
> > }
> >
> > It is unlikely that the initialization of master_thread_id would be 
> > done statically, but I think there's no rule that currently 
> prevents 
> > it.
> 
> (I've added a name to the function)
> 
> Taken in isolation, the initialization of master_thread_id 
> definitely cannot 
> be performed early, because the compiler can't prove that the 
> requirements 
> of 3.6.2/2 are met.
> 
> But if this function is static, and the only calls to it are
> 
> static int x = f( 1 ), 1;
> static int y = f( 2 ), 2;
> 
> at namespace scope, and its address is never taken, then 
> master_thread_id 
> can be initialized statically to 1 (unless something else in 
> f prevents the 
> optimization). 
> 
> 




More information about the cpp-threads mailing list