Re: [boost] Enhanced call_once()
by William E. Kempf other posts by this author
Aug 7 2002 3:33PM messages near this date
Re: [boost] Enhanced call_once()
|
Re: [boost] Enhanced call_once()
----- Original Message -----
From: "Rob Stewart" <stewart@[...].com>
> From: Andrew J Bromage <ajb@[...].net>
> > 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
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Andrew J Bromage
Peter Dimov
Andrew J Bromage
William E. Kempf
Andrew J Bromage
William E. Kempf
Alexander Terekhov
Pete Becker
Andrew J Bromage
Peter Dimov
Andrew J Bromage
William E. Kempf
Andrew J Bromage
William E. Kempf
Rene Rivera
William E. Kempf
Andrew J Bromage
Jason Stewart
Andrew J Bromage
Andrew J Bromage
Rob Stewart
William E. Kempf
William E. Kempf
Alexander Terekhov
Peter Dimov
Peter Dimov
Pete Becker
William E. Kempf
William E. Kempf
Pete Becker
|