tneumann | 1 Aug 2001 09:16
Favicon

Re: Boost.Threads - once functions

> 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); }

williamkempf | 1 Aug 2001 15:56
Picon
Favicon

Re: Boost.Threads - once functions

--- In boost <at> y..., tneumann <at> w... wrote:
> > 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:

I know there's ways to simulate the functionality.  Other wise the 
question wouldn't have been posed for whether or not Boost.Threads 
should have once methods ;).

> 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);
> }

This version is very similar to the pthreads-win32 implementation and 
suffers from two problems, IMHO.  First, like I said in another post 
(Continue reading)


Gmane