Boost logo

Boost :

From: tneumann_at_[hidden]
Date: 2001-08-01 02:16:44


> POSIX uses pthread_once to do this,
> but there's no Win32 equivalent. Instead, Win32 programmers are
> expected to make use of DllMain to handle this sort of
initialization
in Win32 pthread_once can be simulated by using something similar to
the the following:

struct OnceInfo {
   DWORD counter;
   bool done;
};

static OnceInfo info = {-1,false};

void doOnce(OnceInfo& info,void (*foo)())
{
   if (InterlockedIncrement(&info.counter)==0) {
      foo();
      info.done=true;
   } else while (!info.done) Sleep(0);
}

void someFunc() { doOnce(info,foo); }


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