[cpp-threads] RE: volatile, memory models, threads

Peter Dimov pdimov at mmltd.net
Sat Mar 4 01:44:41 GMT 2006


Boehm, Hans wrote:

> If you are filling in a data member of a dynamically allocated class
> on demand, e.g. because it only needs to be computed for a small
> subset of the instances, but needs to be repeatedly accessed for
> those.

class X
{
private:

    T * pt_;
    once_flag ptf_;

    void init_pt( /* args */ )
    {
        pt_ = new T( /*args */ );
    }

public:

    void f()
    {
        // needs *pt_

        call_once( ptf_, &X::init_pt, this /*, args */ );

        // or ptf_.call( ... )

        // use *pt_
    }
};

It's slightly more awkward with pthread_once since it can't pass arguments 
ot invoke member functions, but "our" call_once need not be so limited.

A language/library that doesn't provide a better alternative to DCL is... 
not something to be proud of.




More information about the cpp-threads mailing list