bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another multithreading bug (I think)


From: Wim Oudshoorn
Subject: Re: Another multithreading bug (I think)
Date: Mon, 11 Sep 2006 13:35:19 +0200
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/22.0.50 (darwin)

Richard Frith-Macdonald <richard@tiptree.demon.co.uk> 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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]