cas and pointers

Martin Nowak dawg at dawgfoto.de
Wed Nov 2 11:55:11 PDT 2011


On Tue, 01 Nov 2011 02:42:16 +0100, Martin Nowak <dawg at dawgfoto.de> wrote:

> On Mon, 31 Oct 2011 19:13:57 +0100, Michel Fortin  
> <michel.fortin at michelf.com> wrote:
>
>> On 2011-10-31 17:07:31 +0000, Sean Kelly <sean at invisibleduck.org> said:
>>
>>> writeThis really shouldn't be const, it should be shared.  The problem  
>>> was t
>>> hat making writeThis a shared pass by value param implied a  
>>> synchronized loa
>>> d for writeThis, which is undesirable for cas. So I left the  
>>> declaration of c
>>> as as specified in TDPL and thought I'd figure this out later.
>>
>> Perhaps it should be Unqual!(shared(V2)). That way if V2 is a pointer  
>> it'll be tail-shared.
>>
>> That won't work for class references though. For that you'd need my  
>> "const(Object)ref" patch rusting in DMD's pull requests.
>>
> It's a pitty that cas doesn't work with classes.

Does anybody have a good idiom for lazy initialization of shared classes  
then?
I used to use this one, which doesn't compile any longer.
----------
class Library
{
   void init() {}
}

private shared Library _library;

shared(Library) library()
{
     if (_library is null)
     {
       auto instance = new shared(Library)();
       synchronized(instance)
       {
           if (cas(&_library, cast(Library)null, instance)
               instance.init();
       }
     }
     return _library;
}


More information about the Digitalmars-d mailing list