Boost logo

Boost :

From: William E. Kempf (williamkempf_at_[hidden])
Date: 2002-08-07 10:33:35


----- Original Message -----
From: "Rob Stewart" <stewart_at_[hidden]>
> From: Andrew J Bromage <ajb_at_[hidden]>
> > Moreover (and this is what makes it tricky), I want to do it with as
> > little mutex contention as possible. In particular I want to avoid
> > obtaining a lock when get_expensive() is called after the expensive
> > data has been created.
>
> Is it that tricky? If there was a static pointer that was initialized to
zero
> and was set to the address of the initialized object once ready, then a
simple
> test of the pointer would indicate whether a synchronized check and
possibly
> initialization is needed. Of course you need a static pointer for each
object
> you want call_once to work with, but templates can help there, can't they?
That
> is, we could have the compiler generate a new template specialization for
each
> call_once object created, and those specializations can create the static
> pointer.

It is that tricky. What your suggesting is basically the DCL (double
checked locking) pattern, which is a non-portable solution at best. Memory
visibility rules show that this pattern invokes undefined behavior. I also
don't see how a template could create a static instance per object instance.

> This approach means that near initialization time, call_once may do some
locking
> in multiple threads as they vie to be the one to initialize the data and
wait
> for its initialization. After initialization, no locking would be needed
since
> the pointer will then be non-null. IOW, it isn't quite the ideal you
specified,
> but it's pretty close.

Do a web search on DCL. This topic has been talked to death.

Bill Kempf


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk