Re: using local static variable inside a function
AnonMail2005@gmail.com wrote:
On Sep 30, 8:23 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
Is it advisable to use a local static variable inside a function ? I
want to know whether there are any drawbacks of using local static
variable inside a function.
Kindly explain.
Thanks
V.Subramanian
A static variable inside a function can sometimes be a good idea. A
Meyers singleton is a good example of the use of a static variable
inside a function.
But, and this is a big one, once you move from single threaded apps,
any static variables inside a function run into initialization issues
if more than one thread calls the function.
Static variable inside a function have compiler generated code that
makes sure the object is only constructed and initialized once. Since
standard C++ knows nothing about threads, the mechanism used to ensure
this is not thread safe.
Sorry, but that's a copout. If a compiler supports multiple threads,
then it should provide a thread-safe mechanism for initializing
function-static data objects. If it doesn't, don't blame the standard.
Blame the compiler.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)