Re: One-time initialization of statics in a multithreaded world
Sorry I should have provided a example :)
ONE_TIME_INITIALIZE(std::auto_ptr<int>, sCache, new
int(FonctionComplexe()));
.... is what I want to express as a replacement to:
static std::auto_ptr<int> sCache(new int(FonctionComplexe()));
I want dtors to be called at the end of the code and I don't want to
rely on volatile but only on interlocks....
On 20 juil, 16:51, "Alex Blekhman" <tkfx.REM...@yahoo.com> wrote:
"Bertrand Augereau" wrote:
I cooked a macro for initializing objects once in our
project, could
you review and criticize it, please?
I'm mostly interested in correctness, not performance :)
I'm not sure why do you need all these tricks with static
pointers. I'd make it like this:
#define ONE_TIME_INITIALIZE(TYPE, NAME, INITIALIZER) \
\
static volatile LONG NAME##lock = 0L; \
\
while(InterlockedExchange(&NAME##lock, 1) != 0) \
{ \
Sleep(1); \
} \
\
static TYPE* NAME##Ptr = NULL; \
\
if(!NAME##Ptr) \
{ \
*NAME##Ptr = &INITIALIZER; \
} \
\
TYPE& NAME = *(NAME##Ptr); \
\
InterlockedExchange(&NAME##lock, 0);
Alex