Re: atomically thread-safe Meyers singleton impl (fixed)...
On Jul 30, 4:34 pm, James Kanze <james.ka...@gmail.com> wrote:
On Jul 30, 4:25 pm, "Chris M. Thomasson" <n...@spam.invalid> wrote:
"Anthony Williams" <anthony....@gmail.com> wrote in message
news:uljzj4hf7.fsf@gmail.com...
"Dmitriy V'jukov" <dvyu...@gmail.com> writes:
On Jul 30, 8:44 am, "Chris Thomasson" <x...@xxx.xxx> wrote:
template<typename T>
struct singleton {
static T* instance() {
static T* volatile this_ptr = NULL;
I think here is a little problem. this_ptr is initialized
dynamically, and this initialization is not thread-safe. So
some thread can overwrite pointer in this_ptr with NULL.
You have to made this_ptr global, not function local, so it
will be initialized with NULL statically before any user
code is executed.
Initialization with a constant is still static
initialization, even for function locals.
That's what I always thought. However, perhaps he is thinking
along the lines of:
struct foo {
foo() {
puts("HELLO!");
}
};
void static_me_or_not(int flag) {
if (flag == 666) {
static foo x;
}
}
/* program 1 */
int main() {
static_me_or_not(1);
static_me_or_not(2);
static_me_or_not(3);
static_me_or_not(4);
return 0;
}
/* program 2 */
int main() {
static_me_or_not(1);
static_me_or_not(2);
static_me_or_not(666);
static_me_or_not(4);
return 0;
}
The execution of program 1 will not print "HELLO!", however,
the execution of program 2 will... Sounds dynamic...
Maybe because it is dynamic. Initializing an object with class
type and a user defined constructor is dynamic initialization.
Initializing a pointer with NULL is static initialization. Of
course, the current standard doesn't make this distinction for
local objects; it doesn't have to, since a local object cannot
be seen before the control flow has been executed, and there's
no way a conforming program could tell. Presumably now that
threads are being added to the standard, the distinction will
apply here as well (but I'm not sure that anyone has thought to
look at it yet).
I've looked it up. Interestingly, C gives no real guarantee,
presumably because initialization of an object with static
storage duration can only be with constant expressions, and
without threads, there's no way for a program to see when it
took place. C++ addresses the issue in =A73.6.2; the name of the
section is "Initialization of non-local objects", but it the
first paragraph speaks of "Objects with static storage
duration", with no requirement that they be non-local, and so
applies here: "Objects with static storage duration (3.7.1)
shall be zero-initialized (8.5) before any other initialization
takes place. [..] Together, zero-initialization and constant
initialization are called static initialization; all other
initialization is dynamic initialization. Static initialization
shall be performed before any dynamic initialization takes
place." And there's no way you're going to start a thread from
static initialization (which requires constant epxressions).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34