Boost logo

Boost :

From: williamkempf_at_[hidden]
Date: 2001-09-25 15:42:36


--- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> From: <scleary_at_j...>
> > >
> > > 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.

It's not an idealistic goal at all. And my reading of discussions on
this topic and on DCL indicate that there should not be an issue with
using boost::call_once to do this, though I am disturbed that the
POSIX standard appears to be ambiguous in this regard.

Bill Kempf


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