Is += on strings a thread-safe operation?

Aahz Maruch aahz at panix.com
Wed Feb 28 13:43:22 EST 2001


In article <3a9d3833.737414 at news.btx.dtag.de>,
Stefan Franke <spamfranke at bigfoot.de> wrote:
>
>What about augmented assignments of other built-in types?  Being able
>to thread-safely append to a string buffer (or other sequences) with +=
>would be a big advantage of the somewhat infamous augmented assignment
>operators in my eyes.

Using the dis module, we see that the only difference between "x+='b'"
and "x=x+'b'" lies in substituting INPLACE_ADD for BINARY_ADD.  If the
latter is thread-safe, so is the former; I'm not entirely confident
enough in my byte-code skills to make an assertion.  But it looks to me
that it's not thread-safe because you're reading and assigning the same
variable in the same statement.

I *do* know that x.append('b') is a thread-safe operation -- and that's
probably a better bet, anyway, even if threading isn't involved.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Nostalgia just ain't what it used to be



More information about the Python-list mailing list