G++ and constructors

Daniel Lohmann daniel.lohmann@informatik.uni-erlangen.de
Wed Mar 7 16:40:00 GMT 2007


John Love-Jensen schrieb:
> [...] 
> Although you didn't ask for work-arounds, here's a work-around that works
> for GCC, but may not work in C++ in general*.
> 
> GCC uses threading protection for static objects.
> 
> So this variant of your routine, in GCC, should be thread safe:
> 
> ----------------------------------------------------------------------
> myclass* myclass::init_instance() // private: static
> {
>   return new myclass();
> }
> 
> myclass* myclass::get_instance() // public: static
> {
>   static myclass* instance = InitInstance();
>   return instance;
> }
> ----------------------------------------------------------------------

*GCC* does the threading protection?

I really wonder how!

How can GCC achieve this on every platform (especially embedded targets 
such as AVR, H8, ARM) without help from the runtime? (I haven't seen any 
dependencies to RTL functions in such case.)

Is this implemented using interruption-transparent algorithms / atomic 
operations or does the above simply hold only for some specific platform?


Daniel



More information about the Gcc-help mailing list