Immutable object thread-safety
sumerc at gmail.com
Sun Oct 26 21:59:05 CET 2008
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'
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
st += 'ThreadAWasHere'
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.
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 -
I am confused about which data structure to rely on thread-safety, or
operator in Python?
More information about the Python-list