← Questions

Thread-safe task store with worker dispatch and requeue

0
NuroCodingLast asked Sep 2026

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.

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