Thread safetyness in Python

Skip Montanaro skip at pobox.com
Wed Jul 3 11:34:28 EDT 2002


    Oren> So a+=1 isn't atomic, but l+=[1] is. Interesting.

How so?

    >>> def f(l):
    ...   l += [1]
    ... 
    >>> dis.dis(f)
              0 LOAD_FAST                0 (l)
              3 LOAD_CONST               1 (1)
              6 BUILD_LIST               1
              9 INPLACE_ADD         
             10 STORE_FAST               0 (l)
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        

Looks to me like there's the opportunity for another thread to sneak in
there and modify l between the LOAD_FAST and STORE_FAST instructions...

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html





More information about the Python-list mailing list