Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: deque and thread-safety

Thread Tools

Re: deque and thread-safety

Dieter Maurer
Posts: n/a
Christophe Vandeplas <(E-Mail Removed)> 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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with C++ stl (deque and vector) C++ 4 12-25-2008 12:43 PM
size and iterator of deque George2 C++ 0 03-02-2008 07:43 AM
Popping from the middle of a deque + deque rotation speed Russell Warren Python 5 05-02-2006 06:23 AM
problem with deque and string Rich Herrick C++ 2 12-04-2004 03:03 PM
vector, list and deque Jeff Schwab C++ 4 01-03-2004 04:22 PM