atomic operations in presence of multithreading

Jeff Shannon jeff at ccvcorp.com
Wed Jul 28 20:16:29 CEST 2004


Duncan Booth wrote:

>Paul Moore <pf_moore at yahoo.co.uk> wrote in news:y8l55cwa.fsf at yahoo.co.uk:
>
>  
>
>>Given that the original question was about dictionaries, which are
>>coded in C (and so not subject to this issue) there is still the
>>following case: when the old value stored in the dictionary is
>>replaced, that could be the last reference to it. When the old value
>>is freed, its __del__ method gets called - arbitrary Python code
>>again.
>>    
>>
>
>Right, but the new value has already been stored in the dictionary at the 
>point where the __del__ method is called, so as far as the OP is concerned 
>this is still safe.
>  
>

For some definition of "safe", perhaps.  It won't corrupt any Python 
internals, but if the __del__() method has side effects that the 
threaded code is relying on, it may give unexpected results.  (One might 
want to rely on the fact that, once an object holding a socket is 
removed from a dictionary, that socket will be closed; if that close 
happens in the object's __del__(), then there's no guarantee that it 
*will* actually be closed before a thread switch occurs. (Relying on 
__del__() is hazardous in any case, actually, thanks to differing GC 
between different implementations of Python, but it's even worse in a 
threaded environment.))

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list