From: williamkempf_at_[hidden]
Date: 2001-08-01 08:56:06


--- 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
the pseudo spin lock can cause poor performance if foo() does some
lengthy operations (and by lengthy I just mean enough operations to
be longer than a single time slice). Second, the InterlockedIncrement
() approach may well wrap and cause major problems. This mistake is
easily fixed by using InterlockedExchange() instead.

Bill Kempf