Topic: local static var processing?
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/04/23 Raw View
In article <199904220019.RAA14404@the-top.Eng.Sun.COM>, suku
<Sukumar.Puvvala@eng.sun.com> writes
>Hi,
>
> I am not sure if this belongs to comp.std.c++ ........
>
> static variable definitions, local to a fucntion, are
> initialized/constructed only in the first call. How does the
> compiler check on subsequent calls to the function whether the
> local static var has already been initialized?
Any way the implementor can think of. Usually some kind of flag is used
but it is not the job of the standard to mandate how correct behaviour
is achieved.
>Is there some kind
> of a flag that is set on the first call and on subsequent calls is
> left alone? Is it something more fancy? Any pointers to literature
> on this topic is appreciated.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: suku <Sukumar.Puvvala@eng.sun.com>
Date: 1999/04/22 Raw View
Hi,
I am not sure if this belongs to comp.std.c++ ........
static variable definitions, local to a fucntion, are
initialized/constructed only in the first call. How does the
compiler check on subsequent calls to the function whether the
local static var has already been initialized? Is there some kind
of a flag that is set on the first call and on subsequent calls is
left alone? Is it something more fancy? Any pointers to literature
on this topic is appreciated.
thanks,
sukumar
< Apr 21 1999 17:05 PST >
___________________________________________________________________________
Puvvala Sukumar; 408-774-8063(W); sukumar.puvvala@eng.sun.com
Lonely, at the top!
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/04/22 Raw View
suku <Sukumar.Puvvala@eng.sun.com> writes:
> static variable definitions, local to a fucntion, are
> initialized/constructed only in the first call. How does the
> compiler check on subsequent calls to the function whether the
> local static var has already been initialized? Is there some kind
> of a flag that is set on the first call and on subsequent calls is
> left alone? Is it something more fancy? Any pointers to literature
> on this topic is appreciated.
This is on implementation strategies. I'm not sure whether it is
within the newsgroup charter; such questions are often asked and
answered here.
You can find out yourself by looking at the generated assembler code.
g++ (2.8.1, and any egcs version) emits a global integer variable
parallel to the static variable. This integer variable is
default-initialized to zero (usually as an operating system feature),
and used to check whether initialization has been performed.
There are a couple of problems, though:
a) Setting the flag before or after initialization? If set after,
recursion may occur:
void foo();
struct X{
X(){foo();}
};
void foo(){
static X x;
}
When calling foo, the __x_initialized flag will be zero, so X::X is
called. This calls foo, which checks the flag, etc. If the flag is
set before calling X::X, no recursion will occur
b) If the constructor of the object throws an exception, destructors
for constructed subobjects must be called (as always). However,
is the object then initialized? If the flag is set after
construction, throwing the exception will cause the function to
terminate, the flag is not set. The next time the function is
entered, construction is attempted again.
The standard says
a) Doesn't matter. Behaviour is undefined when variable declaration is
re-entered while variable is constructed. Stack overflow is
acceptable.
b) Construction should be retried after failure
So, setting the flag *after* initialization is the right thing.
Then we have
c) Multithreading. If multiple threads enter the function, and
construction takes some time, what will happen? If the flag is set
before construction, the second thread might access an
uninitialized object. If the flag is set after construction, the
second thread will also attempt construction, thus leading to
inconsistencies.
The standard is silent, and compilers typically regard this as an
application issue. For egcs, I have a patch that will block a second
thread while the first one performs initialization of the static. When
that succeeds, the second thread will continue. If it fails with an
exception, the second thread will re-attempt initialization.
Hope this helps,
Martin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]