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

Bradley Lowekamp blowekamp at mail.nih.gov
Thu Apr 12 13:23:48 EDT 2012


I still have one more question regarding the registration. I see that the following code prevents the factories for getting registered multiple time:


static bool MetaImageIOFactoryHasBeenRegistered;

void MetaImageIOFactoryRegister__Private(void)
{
  if( ! MetaImageIOFactoryHasBeenRegistered )
    {
    MetaImageIOFactoryHasBeenRegistered = true;
    MetaImageIOFactory::RegisterOneFactory();
    }
}

I don't under stand how MetaImageIOFactoryHasBeenRegistered gets initialized before usage. And then is the static initialization fiasco [1] an issue?

[1] http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15


Thanks,
Brad



On Apr 12, 2012, at 11:06 AM, Lowekamp, Bradley (NIH/NLM/LHC) [C] wrote:

> Thanks for the information!
> 
> I clearly need to brush up on when different types of globals and static variables get initialized.
> 
> Brad
> 
> On Apr 12, 2012, at 10:22 AM, Brad King wrote:
> 
>> On 4/12/2012 9:56 AM, Bradley Lowekamp wrote:
>>> the ImageIO Factories are registered by static initialization
>>> in each translation unit which includes tikImageFileReader.h.
>> 
>> That's correct, but only for translation units in applications.
>> 
>> This is the only solution I've found that preserves the pre-
>> modularization registration behavior without requiring every
>> application to *explicitly* call an init function.  The app
>> must do the init because after modularization the base classes
>> cannot magically know about all their implementations in other
>> modules.  The solution is to inject static initalization into
>> translation units of applications.
>> 
>>> C++ does not specify the thread safety of static initialization.
>> 
>> By the time main() runs to start threads other than the main one
>> the static initialization will have completed.  The concern should
>> only exist for dynamically loaded libraries (plugins).  For those
>> an application developer can compile with the option that says
>> not to do the auto init magic.  Just add
>> 
>>  set(ITK_NO_IO_FACTORY_REGISTER_MANAGER 1)
>> 
>> before including ITK in the application CMake code.
>> 
>> -Brad K
> 
> ========================================================
> Bradley Lowekamp  
> Medical Science and Computing for
> Office of High Performance Computing and Communications
> National Library of Medicine 
> blowekamp at mail.nih.gov
> 
> 
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.php
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers

========================================================
Bradley Lowekamp  
Medical Science and Computing for
Office of High Performance Computing and Communications
National Library of Medicine 
blowekamp at mail.nih.gov



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-developers/attachments/20120412/e20e1872/attachment.htm>


More information about the Insight-developers mailing list