5

I've been reading about the thread-safety of CPython's collections.deque here and here. I understand that .append() and .popleft() operations play nice with each other across threads, as do .appendleft() and .pop().

My question is: does the same apply between .popleft() and .clear()? Or failing that, between .popleft() and .pop()? My situation is that I have a consumer thread, call it A, continually .popleft()ing items from a deque d, and a producer thread B that .append()s them on the right. At some point I want thread B to be able to say "cancel all pending items". If I do this with

d.clear()

will it potentially lead to undefined behaviour due to clashes with thread A's .popleft() operations? How about if I say:

while d:
    try: d.pop()
    except: break

instead?

jez
  • 14,867
  • 5
  • 37
  • 64

2 Answers2

2

From the documentation of the api :

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 (emphasis mine)

Which means that you can pop on any side and still enjoy the benefits. As far as the clear() method goes, one can only be sure by looking at the implementation, but i would assume it's implemented just the same way you suggested.

Edit: Also, if i remember correctly from the java implementation of blocking queues, which for all intended purposes should be fairly close to this one, calling clear() on a queue object does not guarantee that when you access it on the next code line, you will find it empty

omu_negru
  • 4,642
  • 4
  • 27
  • 38
0

If I recall, they should not interfere with what you are trying to do as they will place a mutex on the resource and perform any operation independently, and as long as you catch an exception from the resource if it is empty when attempting to pop you should be good to go.

Theodore Howell
  • 419
  • 3
  • 12