[Insight-developers] Thread safety concerns with IO Factory registration

Brad King brad.king at kitware.com
Thu Apr 12 13:51:17 EDT 2012


On Thu, Apr 12, 2012 at 1:23 PM, Bradley Lowekamp
<blowekamp at mail.nih.gov> wrote:
> static bool MetaImageIOFactoryHasBeenRegistered;
>
> I don't under stand how MetaImageIOFactoryHasBeenRegistered gets initialized
> before usage. And then is the static initialization fiasco [1] an issue?

When the type of a global is a plain-old-data (POD) type the compiler
default-initializes it to zero (false for bool).  We purposely take
advantage of default initialization because the code that
modifies/tests the variable may be invoked during static initalization
of *another* translation unit.  If we had an explicit initializer then
it would overwrite whatever work had been done so far (from other
translation units) when the translation unit defining the global is
explicitly initialized.

This approach is the same as some vendors' standard libraries use to
initialize singletons like "cout" and "cerr" AFAIK.

-Brad K


More information about the Insight-developers mailing list