Topic: Initialization of local static objects
Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: Tue, 24 Jan 2006 19:47:45 +0000 (UTC) Raw View
kanze wrote:
> > > > > > In particular, 6.7p4 contains the following
> > > > > > text: A local object of POD type (3.9) with
> > > > > > static storage duration initialized with
> > > > > > constant-expressions is initialized before its
> > > > > > block is first entered.
>
> > > > > That's interesting, because 3.6.2 says "Objects of POD
> > > > > types with static storage duration initialized with
> > > > > constant expressions shall be initialized before any
> > > > > dynamic initialization takes place." Note the absence
> > > > > here of any "defined in namespace scope", which is
> > > > > present in the description of dynamic initialization.
>
> > > > IMHO this statement is ambiguous. One of possible
> > > > interpretations is: "Local objects of POD types with
> > > > static storage duration initialized with constant
> > > > expressions shall be initialized before any dynamic
> > > > initialization of local objects of the same scope."
>
> > > I don't see it. I think the statement is quite clear:
> > > "[all] objects of POD types with static storage duration
> > > initialized with constant expressions shall be initialized
> > > before ANY dynamic initialization takes place." It's hard
> > > to be clearer.
>
> > James, 3.6.2 has "Initialization of non-local objects" caption.
>
> Because that's what it covers in detail. Section headings don't
> mean that we can ignore statements in the section which concern
> other things.
>
> Still, if you see a problem, it might be worth raising the issue
> before the committee. (The fact that intelligent readers
> interprete the statements differently suggests that the text
> might not be as clear as it should be.)
>
IMHO the sentence above from 3.6.2 is not applicable to local static objects.
In particular, 3.6.2p1 ends with: "The initialization of local static objects
is described in 6.7." note. That is we have to look at 6.7.
And 6.7p4 clearly stands: "A local object of POD type (3.9) with static
storage duration initialized with constant-expressions is initialized before
its block is first entered. An implementation is permitted to perform early
initialization of other local objects with static storage duration under the
same conditions that an implementation is permitted to statically initialize
an object with static storage duration in namespace scope (3.6.2). Otherwise
such an object is initialized the first time control passes through its
declaration;".
That is:
1. An implementation is permitted to initialize a local static object at the
first time control passes through its declaration.
2. And therefore local static pthread_mutex_t mutex =
PTHREAD_MUTEX_INITIALIZER ; is (potentially) thread-unsafe.
Are there any comments?
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Wed, 25 Jan 2006 21:12:17 GMT Raw View
Sergey P. Derevyago wrote:
> And 6.7p4 clearly stands: "A local object of POD type (3.9) with static
> storage duration initialized with constant-expressions is initialized before
> its block is first entered. An implementation is permitted to perform early
> initialization of other local objects with static storage duration under the
> same conditions that an implementation is permitted to statically initialize
> an object with static storage duration in namespace scope (3.6.2). Otherwise
> such an object is initialized the first time control passes through its
> declaration;".
>
> That is:
> 1. An implementation is permitted to initialize a local static object at the
> first time control passes through its declaration.
Unless the object is of POD type initialized with a constant-expression,
because it's clearly stated in the sentence you quote that in that case
it must be "initialized before its block is first entered".
> 2. And therefore local static pthread_mutex_t mutex =
> PTHREAD_MUTEX_INITIALIZER ; is (potentially) thread-unsafe.
No, because pthread_mutex_t is of POD type and PTHREAD_MUTEX_INITIALIZER
is a constant expression, so we are in the exceptional case above.
Ganesh
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Thu, 26 Jan 2006 18:12:09 GMT Raw View
Alberto Ganesh Barbati wrote:
> > And 6.7p4 clearly stands: "A local object of POD type (3.9) with static
> > storage duration initialized with constant-expressions is initialized before
> > its block is first entered. An implementation is permitted to perform early
> > initialization of other local objects with static storage duration under the
> > same conditions that an implementation is permitted to statically initialize
> > an object with static storage duration in namespace scope (3.6.2). Otherwise
> > such an object is initialized the first time control passes through its
> > declaration;".
> >
> > That is:
> > 1. An implementation is permitted to initialize a local static object at the
> > first time control passes through its declaration.
>
> Unless the object is of POD type initialized with a constant-expression,
> because it's clearly stated in the sentence you quote that in that case
> it must be "initialized before its block is first entered".
>
OK, let's put it another way. The phrase "before its block is first entered"
doesn't mean "before its surrounding function is first entered":
void f()
{
// obj can be initialized here, can't it?
{
POD obj=some_constant_expression;
}
}
Also, the phrase "before its block is first entered" from 6.7p4 doesn't equal
to the phrase "Objects of POD types (3.9) with static storage duration
initialized with constant expressions (5.19) shall be initialized before any
dynamic initialization takes place" from 3.6.2p1.
So local static mutex = PTHREAD_MUTEX_INITIALIZER initialization is
dangerous.
> > 2. And therefore local static pthread_mutex_t mutex =
> > PTHREAD_MUTEX_INITIALIZER ; is (potentially) thread-unsafe.
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Alberto Ganesh Barbati <AlbertoBarbati@libero.it>
Date: Fri, 27 Jan 2006 01:56:11 CST Raw View
Sergey P. Derevyago ha scritto:
> Also, the phrase "before its block is first entered" from 6.7p4 doesn't equal
> to the phrase "Objects of POD types (3.9) with static storage duration
> initialized with constant expressions (5.19) shall be initialized before any
> dynamic initialization takes place" from 3.6.2p1.
Although subclause 3.6.2 has title "Initialization of non-local
objects", the sentence you quote (from paragraph 1) applies to *all*
objects of POD types with static duration, so, perhaps surprisingly, it
applies also to local objects. At least that's my interpretation of the
sentence. This does not conflict with 6.7/4 although the wording there
is much weaker, as you noticed.
> So local static mutex = PTHREAD_MUTEX_INITIALIZER initialization is
> dangerous.
Not if my interpretation is correct.
Ganesh
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Fri, 27 Jan 2006 15:41:29 GMT Raw View
Alberto Ganesh Barbati wrote:
> > Also, the phrase "before its block is first entered" from 6.7p4 doesn't equal
> > to the phrase "Objects of POD types (3.9) with static storage duration
> > initialized with constant expressions (5.19) shall be initialized before any
> > dynamic initialization takes place" from 3.6.2p1.
>
> Although subclause 3.6.2 has title "Initialization of non-local
> objects", the sentence you quote (from paragraph 1) applies to *all*
> objects of POD types with static duration,
>
Does it? I think it doesn't.
In particular:
1. The section 3.6.2 has "Initialization of non-local objects" caption.
2. The paragraph 3.6.2p1 explicitly notes: The initialization of local static
objects is described in 6.7.
> so, perhaps surprisingly, it applies also to local objects.
>
No, it does not, IMO.
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]