I have a function that relies on a Queue and I would like to have it multithreaded, where multiple workers put and get items from the same queue. The worker function has the njit(nogil=True) decorator. As far as I know numba does not natively support any Queue class.
I have tried implementing a simple queue class myself, but I cant find a proper way to implement a mutex lock in python, which is necessary to avoid race conditions. Below is my Lock class:
@jitclass([("_locked", types.bool)])
class Lock:
"""Simple boolean lock for synchronizing multiple threads."""
def __init__(self):
"""Initialize lock."""
self._locked = False
def acquire(self):
"""Acquire lock once available."""
# Wait until free
while self._locked:
with objmode:
time.sleep(sleep_time)
self._locked = True
def release(self):
"""Release lock."""
assert self._locked
self._locked = False
When I test this in a multithreading pool with nogil workers I get an AssertionError from release, which means multiple threads must have acquired the same lock. Is there a better way to get a mutex lock, maybe directly from a C library?