deque and thread-safety

Dieter Maurer dieter at handshake.de
Sat Oct 13 02:35:19 EDT 2012


Christophe Vandeplas <christophe at vandeplas.com> 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.




More information about the Python-list mailing list