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

Peter Dimov pdimov at mmltd.net
Sat Mar 4 02:51:39 GMT 2006


Boehm, Hans wrote:

> Maybe.  You probably need another variant where the test is on a NULL
> pointer.  And I would probably find the explicit DCL version at least
> as easy to read.

I didn't show the pointer variant because the current example is more 
realistic, even though it doesn't look as good (separate once_flag, less 
readable.) In C++ without GC, pt_ will probably be a smart pointer. (As an 
aside, I think that you can't do DCL on a smart pointer even with volatile, 
so you'll need a separate once flag too.)

But here it is, for completeness.

class X
{
private:

    T * pt_;

    static T * init_pt( /* args */ )
    {
        return new T( /*args */ );
    }

public:

    void f()
    {
        assign_pointer_once( pt_, init_pt, /*, args */ );

        // use *pt_
    }
};

> And you're making some strong compiler assumptions, I suspect.

I'm not sure how to interpret that remark. I am not making compiler 
assumptions, I am making assertions (as does the hypothetical future DCL, 
whether implemented with volatile or atomics.)

DCL today makes assumptions. pthread_once makes assumptions. 
pthread_mutex_lock makes assumptions. But our future mutex locks won't make 
compiler assumptions, I hope. 




More information about the cpp-threads mailing list