|
Boost : |
From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-11-02 20:41:42
Chris Thomasson wrote:
> Here is one without a ctor:
It still has a dtor. Same as Anmthony already said applies to dtor too.
You will end up needing to drop the class and use a function with a
flag I suspect.
Btw.: The usage scenario you were showing earlier is not quite what
we had in mind. We were speaking about local statics.
In your example usage, you have moved the static part
to global namespace. There are far more less problems.
The problems are getting nasty when needing local statics
as in
void foo()
{
static bar mybar;
}
You really might need them, because the static possibly
depending an a template parameter, which makes it impossible
to move it to global namespace.
So you will need to be able to declare the once class inside the
function body scope.
Back to ctor/dtor. Once you have found out that ctors and dtors
are bad, what is left? Well several options:
1) a once function and a flag (instead of a class)
2) a static initializeable mutex
1) and 2) are roughly equivalent in that one can be built out of
the other.
1) has been used in pthreads (before a static mutex type was available)
Boost.Thread also uses this also mainly in implementation code.
2) I have tried to find out if it is possible to implement a mutex
that is statically initializeable with the additional constraint
that zero initialization suffices.
I have provided a prototype that let me believe it really can be
done. (Such a mutex has the additional benefit that you need no
explicit memory management.)
Roland
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk