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?