Argonne National Laboratory

Experimental Physics and
Industrial Control System

2002  2003  2004  2005  2006  2007  2008  2009  2010  <20112012  2013  2014  2015  2016  2017  2018  2019  2020  Index 2002  2003  2004  2005  2006  2007  2008  2009  2010  <20112012  2013  2014  2015  2016  2017  2018  2019  2020 
<== Date ==> <== Thread ==>

Subject: Re: c++ static initialization
From: "Hill, Bruce" <bhill@slac.stanford.edu>
To: "benjamin.franksen@helmholtz-berlin.de" <benjamin.franksen@helmholtz-berlin.de>, "core-talk@aps.anl.gov" <core-talk@aps.anl.gov>
Date: Tue, 9 Aug 2011 15:50:43 -0700
Good points, Ben.   I concur.

- Bruce

----- Reply message -----
From: "Benjamin Franksen" <benjamin.franksen@helmholtz-berlin.de>
Date: Tue, Aug 9, 2011 3:25 pm
Subject: c++ static initialization
To: "core-talk@aps.anl.gov" <core-talk@aps.anl.gov>

Hi Jeff

you asked for opinions, here is mine:

Am Dienstag, 9. August 2011, um 22:42:37 schrieb Jeff Hill:
> Ref: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
>
> Of course C++ is really different from C in that some significant code can
> run during static initialization. The above FAQ mentions how to avoid some
> really serious issues. All of this is pretty common knowledge, but one has
> to be careful, and I have definitely seen this bug biting in EPICS base
> before.

(1) Static objects should be avoided wherever possible.

(2) If static objects are used, the constructor should be trivial, i.e. not
perform anything except setting members to default values.

(3) Non-trivial initialization for complex objects should be done in a regular
method either lazily ("Construct On First Use") or by an explicit call from
some startup routine. The latter has the advantage of a well-defined order of
initialization; the former is sometimes more convenient.

> However there is still a related issue which should be well understood; not
> everyone agrees if threads should be started during C++ static
> initialization or not.

(4) Starting threads inside static initialization is a bad idea and should not
be allowed (see rule 2).

> If multiple threads are running during c++ static
> initialization then the solution in the above FAQ doesn't apply and the
> only solution is epicsThreadOnce,

(5) epicsThreadOnce should also be avoided wherever possible. Using it means
objects cannot be freely passed between threads which is detriment to
composability.

Cheers
Ben

________________________________

Helmholtz-Zentrum Berlin für Materialien und Energie GmbH

Mitglied der Hermann von Helmholtz-Gemeinschaft Deutscher Forschungszentren e.V.

Aufsichtsrat: Vorsitzender Prof. Dr. Dr. h.c. mult. Joachim Treusch, stv. Vorsitzende Dr. Beatrix Vierkorn-Rudolph
Geschäftsführer: Prof. Dr. Anke Rita Kaysser-Pyzalla, Dr. Ulrich Breuer

Sitz Berlin, AG Charlottenburg, 89 HRB 5583

Postadresse:
Hahn-Meitner-Platz 1
D-14109 Berlin

http://www.helmholtz-berlin.de



Navigate by Date:
Prev: Re: c++ static initialization Benjamin Franksen
Next: Re: c++ static initialization Eric Norum
Index: 2002  2003  2004  2005  2006  2007  2008  2009  2010  <20112012  2013  2014  2015  2016  2017  2018  2019  2020 
Navigate by Thread:
Prev: RE: c++ static initialization Jeff Hill
Next: c++ 0x initialization of local (block scoped) static variables Jeff Hill
Index: 2002  2003  2004  2005  2006  2007  2008  2009  2010  <20112012  2013  2014  2015  2016  2017  2018  2019  2020 
ANJ, 02 Feb 2012 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·