[cpp-threads] Memorymodelcounterproposal:synchronizationoperations

Boehm, Hans hans.boehm at hp.com
Fri May 27 21:07:16 BST 2005


As Alexander implies, and is pointed out in

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

this only works if the object being lazily initialized is static.
If you want to lazily initialize a field of a particular kind of
object, of which there may be many, you need a different mechanism.

Alexander's suggestion should work, but I think you at least
need multiple variants for the pointer vs. flag cases.  And all
of these approaches seem to look significantly different across C, C++,
and
Java, which I think is a big problem in a world in which colleges
will allocate a small number of hours to teach people about
multithreaded programming, and most of those people will go
and program in more than one language.

I still think we should revisit this after the dust settles, so
we'll understand precisely what the tradeoffs are.

Hans

> -----Original Message-----
> From: 
> Cpp-threads_decadentplace.org.uk-bounces at decadentplace.org.uk 
> [mailto:Cpp-threads_decadentplace.org.uk-bounces at decadentplace
> .org.uk] On Behalf Of Alexander Terekhov
> Sent: Friday, May 27, 2005 12:26 PM
> To: cpp-threads at decadentplace.org.uk
> Subject: Re: [cpp-threads] 
> Memorymodelcounterproposal:synchronizationoperations
> 
> 
> On 5/27/05, Peter Dimov <pdimov at mmltd.net> wrote:
> [...]
> > Typical DCL:
> > 
> > X * volatile p = 0;
> > mutex mx;
> > 
> > X * get()
> > {
> >    if( p == 0 )
> >    {
> >        mx.lock();
> > 
> >        if( p == 0 )
> >        {
> >            p = new X;
> >        }
> > 
> >        mx.unlock();
> >    }
> > 
> >    return p;
> > }
> > 
> > Idiomatic MT C++ replacement:
> > 
> > X * get()
> > {
> >    /*protected*/ static X * p = new X;
> 
> static(synchronized) X * p = new X;
> 
> >    return p;
> > }
> 
> Plus future_std::once<T> functor for dynamic context. 
> 
> regards,
> alexander.
> 
> -- 
> cpp-threads mailing list
> cpp-threads at decadentplace.org.uk 
> http://decadentplace.org.uk/mailman/listinfo/cpp-threads_decad
entplace.org.uk




More information about the cpp-threads mailing list