Re: deque and thread-safety

Discussion in 'Python' started by Dieter Maurer, Oct 13, 2012.

  1. Christophe Vandeplas <> writes:

    > ...
    > From the documentation I understand that deques are thread-safe:
    >> Deques are a generalization of stacks and queues (the name is pronounced “deckâ€
    >> and is short for “double-ended queueâ€). Deques support thread-safe, memory
    >> efficient appends and pops from either side of the deque with approximately the
    >> same O(1) performance in either direction.

    >
    > It seems that appending to deques is indeed thread-safe, but not
    > iterating over them.


    You are right.

    And when you think about it, then there is not much point in striving
    for thread safety for iteration (alone).
    Iteration is (by nature) a non atomic operation: you iterate because
    you want to do something with the intermediate results; this "doing"
    is not part of the iteration itself.
    Thus, you are looking for thread safety not for only the iteration
    but for the iteration combined with additional operations (which
    may well extend beyond the duration of the iteration).

    Almost surely, the "deque" implementation is using locks
    to ensure thread safety for its "append" and "pop". Check whether
    this lock is exposed to the application. In this case, use
    it to protect you atomic sections involving iteration.
     
    Dieter Maurer, Oct 13, 2012
    #1
    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. Russell Warren
    Replies:
    5
    Views:
    432
    Tim Peters
    May 2, 2006
  2. NvrBst
    Replies:
    31
    Views:
    1,644
    James Kanze
    Sep 3, 2008
  3. Christophe Vandeplas

    deque and thread-safety

    Christophe Vandeplas, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    107
    Christophe Vandeplas
    Oct 12, 2012
  4. Christophe Vandeplas

    Re: deque and thread-safety

    Christophe Vandeplas, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    111
    Christophe Vandeplas
    Oct 12, 2012
  5. Antoine Pitrou

    Re: deque and thread-safety

    Antoine Pitrou, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    149
    Antoine Pitrou
    Oct 12, 2012
Loading...

Share This Page