threads, mutual exclusion, and lists

Discussion in 'Python' started by Scott, Aug 15, 2007.

  1. Scott

    Scott Guest

    I have two threads that share a python list. One thread adds to the
    list with append(), the other thread removes items with pop().

    My question is -- are python list operations atomic? If they are not,
    then I assume I need to put some mutual exclusion around the append()
    and pop() calls ?

    Thanks,
    Scott
    Scott, Aug 15, 2007
    #1
    1. Advertising

  2. Scott

    Guest

    On Aug 15, 1:36 pm, Scott <> wrote:
    > I have two threads that share a python list. One thread adds to the
    > list with append(), the other thread removes items with pop().
    >
    > My question is -- are python list operations atomic? If they are not,
    > then I assume I need to put some mutual exclusion around the append()
    > and pop() calls ?
    >
    > Thanks,
    > Scott


    You might want to consider using a Queue instead. It is designed to be
    thread-safe.
    , Aug 15, 2007
    #2
    1. Advertising

  3. > I have two threads that share a python list. One thread adds to the
    > list with append(), the other thread removes items with pop().
    >
    > My question is -- are python list operations atomic?


    Yes, they are in the current implementation of CPython (all versions).
    Notice that not *all* operations are atomic (e.g. .sort() is not),
    but both append and pop happen to be atomic (which is primarily because
    they don't need to call back to user-defined functions, unlike sort).

    It's not a language property, though; things may be different in Jython
    or IronPython.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 15, 2007
    #3
  4. > My question is -- are python list operations atomic? If they are not,
    > then I assume I need to put some mutual exclusion around the append()
    > and pop() calls ?


    They are not, but there is one included in the standard library:
    http://docs.python.org/dev/lib/module-Queue.html

    Matt
    Matt McCredie, Aug 16, 2007
    #4
  5. >> My question is -- are python list operations atomic? If they are not,
    >> then I assume I need to put some mutual exclusion around the append()
    >> and pop() calls ?

    >
    > They are not, but there is one included in the standard library:
    > http://docs.python.org/dev/lib/module-Queue.html


    Why do you think they are not?

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 16, 2007
    #5
  6. > Why do you think they are not?

    Because they aren't. You even mentioned that a few operations that
    aren't atomic. If operations are atomic it isn't necessarily because
    of the design of the list, but the design of CPython. More
    specifically the GIL. I don't mean to imply that you can't get a
    multi-threaded app to communicate using lists, but the Queue is
    explicitly built for it and better suited.

    Matt
    Matt McCredie, Aug 16, 2007
    #6
  7. >> Why do you think they are not?
    >
    > Because they aren't. You even mentioned that a few operations that
    > aren't atomic.


    OTOH, the OP specifically asked for .append() and .pop(), which are
    atomic.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 16, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giambattista Bloisi

    non-blocking mutual exclusion

    Giambattista Bloisi, Dec 16, 2003, in forum: Java
    Replies:
    1
    Views:
    403
    Matt Humphrey
    Dec 16, 2003
  2. ppp
    Replies:
    1
    Views:
    405
    Patricia Shanahan
    Dec 5, 2006
  3. ppp
    Replies:
    0
    Views:
    343
  4. ppp
    Replies:
    0
    Views:
    333
  5. Replies:
    4
    Views:
    1,282
    CBFalconer
    Jul 7, 2005
Loading...

Share This Page