private static instance construction not thread-safe?

Joe Buehler jbuehler@hekimian.com
Wed Jul 31 08:22:00 GMT 2002


The attached C++ generates code that is not thread-safe,
when using the gcc 2.95 prebuilt for Cygwin and installable
via the Cygwin installer.

gcc inserts code at the top of f() to take care of construction
of the "temp" instance, and to schedule its destruction at program
exit time, using atexit().

The code that does this is not thread-safe -- two calls to f()
around the same time can cause double-construction, double
calls to atexit(), etc.

Is this a gcc bug, or a result of the way that the gcc I am using
has been configured?  Or is some gcc option supposed to be used
that will take care of this?

My apologies if this has been asked a million times -- I did not
find anything in GNATS for gcc.

Joe Buehler

struct x {
   int i;
   x() {
     i = 0;
   }
   ~x() {
     i = -1;
   }
};

int
f()
{
   static x temp;
   return(temp.i);
}



More information about the Gcc-bugs mailing list