Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-09-25 12:16:13


From: <scleary_at_[hidden]>
> >
> > static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
>
> Boost.Threads is designed to be portable. There is no way to do this
under
> Win32 to my knowledge :(

Looking at the pthreads-win32 source for inspiration, I can imagine
something like:

struct static_mutex
{
    long once;
    CRITICAL_SECTION * pcs;
};

#define BOOST_STATIC_MUTEX_INITIALIZER { -1, 0 }

void lock(static_mutex & m)
{
    if(m.pcs == 0)
    {
        if(::InterlockedIncrement(&m.once) == 0) // first post!
        {
            CRITICAL_SECTION * tmp = new CRITICAL_SECTION;
            ::InitializeCriticalSection(tmp);
            m.pcs = tmp;
        }
        else while(m.pcs == 0)
        {
            ::Sleep(0);
        }
    }

    ::EnterCriticalSection(m.cs);
}

Anyway, my question was about implementing a singleton with Boost.Threads.
;-)

IMO, anything that is easy to do under pthreads should be at least as easy,
and less error-prone, under Boost.Threads, otherwise why bother? The
singleton is an example that fails to meet that (perhaps idealistic) goal.

--
Peter Dimov
Multi Media Ltd.

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