← Questions
Thread-safe task store with worker dispatch and requeue
0
I was asked this
No reports yet. Sign in to say where you were asked it.
Build a task store that many threads use at once. Tasks are queued, running or completed, and each worker runs at most one task at a time.
-
add(task)queues a new task. -
dispatch(worker)hands the worker the oldest queued task, or nothing when none is queued. -
complete(worker, task)marks it done and frees the worker; completing the same task twice is harmless. -
fail(worker, task)frees the worker and puts the task back in the queue at its original position, not at the end;release(worker)does the same for whatever the worker is running.
Make every call atomic and explain how you keep the queue order and avoid races.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
