Richard Frith-Macdonald <[EMAIL PROTECTED]> writes:

> - (id) sharedInstance
> {
>    static volatile uint8_t    isCreated = 0;  // single byte
>    declared  volatile so we read from memory rather than a register
>    static volatile id theInstance = nil;
>
>    if (isCreated == 0)
>      {
>        [theLock lock];        
>        if (isCreated == 0 && theInstance == nil)
>          {
>            theInstance = [theClass new];
>          }
>        [theLock unlock];
>        isCreated = 1;
>     }
>    return theInstance;
> }


I am not sure this is safe on a multiprocessor machine
Becauses I think that on a multiprocessor machine the following might happen:

Thread A on Processor A creates the instance and continues
Thread B on Processor B sees isCreated = 1, but still has a stale
            value (nil) for 'theInstance' in its L1/L2 memory cache
            and returns the nil.

But I don't know enough about the memory model that the current
processors use and how that interacts with locking.  

Wim Oudshoorn.


_______________________________________________
Bug-gnustep mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnustep

Reply via email to