thread-safe local static variable initialization

Christophe de Dinechin ddd at cup.hp.com
Tue Jun 8 18:26:11 UTC 1999


Jim,


> It's not obvious to me that compilers have to do it.  As Mike points 
> out, it is permissible to leave the responsibility to the users.  And, 
> I'm inclined to believe that's the right solution.  What it would take 
> to change my mind would be a solution which addressed the following 
> concerns:

I sent another message explaining why I think compilers _should_ do  
it, even if they don't _have_ to.


> First, it should not cause noticible cost in non-multithreaded
> programs.

Our implementatino has some cost at initialization time (the cost of  
locking). However, the process also sets a static boolean flag, that  
is then tested. Also note that there is an initialization cost  
anyway because you need to register the object for destruction.

After that, the cost is that of checking a flag, whether in threaded  
or non-threaded mode.


> Second, it should not cause unnecessary cost in multithreaded programs. 
> This requires more elaboration.  Even in multithreaded programs, I 
> would guess that most relevant initializations would not really be 
> exposed to multiple simultaneous executions, and a careful user could 
> avoid locking.  Further, in many of the remaining cases, I would expect 
> a careful user to be able to use a single lock for a collection of 
> object initializations, giving the per-object locking I would expect 
> from an automatic approach higher overhead than necessary.

There is a cost in multithreaded programs, because we some threads  
may be blocked while attempting a static initialization that is  
completing in another thread. The cost after initialization is  
limited to a single flag check, though. And I think this cost is  
minor.


> However, it's not very effective to discuss the problem in the
> abstract.  If someone has a specific proposal for solving the problem, 
> submit it, and we can discuss concrete characteristics instead of
> speculations.

Maybe we can present what HP aC++ does. Any other compiler has a  
similar mechanism?


Regards
Christophe




More information about the cxx-abi-dev mailing list