Immutable object thread-safety
Alcari The Mad
AlcariTheMad at gmail.com
Mon Oct 27 02:25:09 CET 2008
> Hi all,
> This will probably be a long question/short answer, sorry, but I have
> wandered net about the subject and really feel cannot find just enough
> information.I want to ask my question by giving an example to
> explicitly express my understanding which may be also wrong:
> So, let's have a string in a module like:
> st = 'IAmAstring'
When you make an assignment like that, python generates bytecode that
looks like this(straight from dis.dis):
1 0 LOAD_CONST 0 ('IAmAstring')
3 STORE_NAME 0 (a)
That's 1 instruction to load the constant and another one to bind it.
> My understanding: Python allocates a string object and binds the st to
> that string's reference.
> and let's have two threads simply doing following operations on the st
> Thread A:
> st += 'ThreadAWasHere'
> Thread B
> st = 'ThreadBWasHere'
> Let's think about the following situation:
> Thread A reads the st value, and it is currently binded to an object
> which is holding the string 'IAmAString'.
> So let Thread B starts execution at that point. Note that Thread A
> does not add the new string to object, it only reads st object. So,
> then Thread B just writes to st, and all of a sudden st is binded to
> something which is ThreadBWasHere. And then let's continue Thread A,
> it already readed st's reference which is a reference to 'IamAString'.
> So, finally if the execution flow is like above, we will have
> 'IAmAStringThreadAWasHere'. I am assuming this should not be the case.
> We should have 'ThreadBWasHereThreadAWasHere' in the final string,
> because we have just consumed the code in ThreadB.
Assuming that thread A and B contain nothing but that code, you will
either get 'ThreadBWasHereThreadAWasHere' or just 'ThreadBWasHere',
because by default the interpreter switches threads every 100 bytecode
> Also, the other question is the operation st = 'ThreadBWasHere' is
> atomic? I mean, if Python does not guarantee if an immutable object
> assignment is atomic, then how can we say that the object is thread-
> safe? So, if it is an atomic operation, which operators are atomic,
> means only assignment'='? How about other operators like +=, or -
The augmented assignment operators first load the current value of the
variable like this:
a += 'asdf'
1 0 LOAD_NAME 0 (a)
3 LOAD_CONST 0 ('asdf')
7 STORE_NAME 0 (a)
> I am confused about which data structure to rely on thread-safety, or
> operator in Python?
All of the builtin functions(which are implemented in C, like len()) are
atomic(but assigning their output to a value may not be).
I hope this helps,
More information about the Python-list