FAQ
Someone recommended using the idiom

spam, eggs = eggs, spam

to get a thread-safe swap. Does this really work?
import dis
def x(a, b): a, b = b, a
...
dis.dis(x)
0 SET_LINENO 1

3 SET_LINENO 1
6 LOAD_FAST 1 (b)
9 LOAD_FAST 0 (a)
12 BUILD_TUPLE 2
15 UNPACK_SEQUENCE 2
18 STORE_FAST 0 (a)
21 STORE_FAST 1 (b)
24 LOAD_CONST 0 (None)
27 RETURN_VALUE

So if this thread loses control anywhere between the first LOAD_FAST
and the last STORE_FAST, a value could get stored by another thread
into "b" which would then be lost. There isn't anything keeping this
from happening, is there?

Search Discussions

  • David Bolen at Mar 27, 2002 at 10:16 pm

    Kragen Sitaker <kragen at pobox.com> writes:

    Someone recommended using the idiom

    spam, eggs = eggs, spam

    to get a thread-safe swap. Does this really work? (...)
    So if this thread loses control anywhere between the first LOAD_FAST
    and the last STORE_FAST, a value could get stored by another thread
    into "b" which would then be lost. There isn't anything keeping this
    from happening, is there?
    Nope. In general not even a simple assignment is necessarily thread
    safe since performing the assignment may invoke special methods on an
    object which themselves may require a number of operations. Hopefully
    the object will have internally locked its "state" values, but that's
    not always the case.

    But it's really dictated by what "thread safety" means in a particular
    application, because to my mind there are many levels of granularity
    of such safety so it's hard to talk about "thread safety". About the
    only thing the Python interpreter is going to give you for free is
    that a built-in data type should be safe from internal corruption even
    with native threading. In other words if two threads have "a=0xff"
    and "a=0xff00", a will end up with one or the other, but not
    accidentally "0xffff" as might be possible in some other languages if
    a isn't protected.

    With that said, Python also tends to execute in such a fashion that
    you can get away with an awful lot without formal locking, if you're
    willing to live on the edge a bit and have implied dependencies on the
    actual objects in use. There was a decent discussion along those
    lines here in c.l.p a while back - search groups.google.com for the
    "Critical sections and mutexes" thread among others.

    Personally, I explicitly lock shared state (or use constructs designed
    for exchanging shared information properly amongst threads, such as
    Queue.Queue) in any multi-threaded application. To my mind it's the
    best protection against maintenance and evolution down the road.

    --
    -- David
    --
    /-----------------------------------------------------------------------\
    \ David Bolen \ E-mail: db3l at fitlinxx.com /
    FitLinxx, Inc. \ Phone: (203) 708-5192 |
    / 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
    \-----------------------------------------------------------------------/
  • Aahz at Mar 27, 2002 at 11:44 pm
    In article <833cyl3gfe.fsf_-_ at panacea.canonical.org>,
    Kragen Sitaker wrote:
    Someone recommended using the idiom

    spam, eggs = eggs, spam

    to get a thread-safe swap. Does this really work?
    Not really. IIRC, there are some specific circumstances where it does
    work, but better to protect it with an RLock(). OTOH, it's safe if spam
    and eggs are both local.
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    Why is this newsgroup different from all other newsgroups?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 27, '02 at 8:27p
activeMar 27, '02 at 11:44p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase