Re: atomically thread-safe Meyers singleton impl (fixed)...
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).
--
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