Tips for Thread Safety? #32
-
Hi, I'm considering using this data struct for a project. The exposed interfaces (heap queue + dictionary access) and implementation are appealing, but it looks like it may not be thread-safe. Any suggestions on what it would take to implement thread-safe behavior for the pqdict object? I plan to have mostly multiple writers at any given time. I might have to introduce a lock variable and add it to the main methods in the class whenever the items are added or removed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Very late response, but you are right that this is not a thread-safe implementation. However, if you take a look at the standard library queue module, you can see that the It would make a nice recipe to add to the examples. |
Beta Was this translation helpful? Give feedback.
Very late response, but you are right that this is not a thread-safe implementation.
However, if you take a look at the standard library queue module, you can see that the
Queue
base class implements all of the locking required for thread safety. It should be possible to derive something similar to the stdlibqueue.PriorityQueue
that wraps apqdict
. To expose extra functionality like looking up an item by key or modifying a priority, you would have to add additional methods that acquire locks similar to the implementation ofQueue.get
.It would make a nice recipe to add to the examples.