Interview conceptCoding & Algorithms

Thread-Safe Queues And Concurrency Primitives

Asked of: Software Engineer

Last updated

Editorial architecture diagram: producers -> bounded blocking queue (deque, mutex, condition vars, closed flag) -> consumers, with callouts for condition-variable loop, timed waits, shutdown, and common pitfalls.

What's being tested

This tests concurrent data structure design: implementing FIFO queues/buffers that remain correct under multiple producers and consumers. Interviewers probe whether you can use mutexes, condition variables, semaphores, and shutdown semantics without races, deadlocks, busy-waiting, or lost wakeups.

Patterns & templates

  • Bounded blocking queue — store items in collections.deque; put() waits while full, get() waits while empty; both are O(1).

  • Condition-variable loop — always call wait() inside while not predicate; handles spurious wakeups, missed notifications, and predicate changes after reacquiring lock.

  • Producer–consumer template — one lock protects queue state; not_empty.notify() after enqueue, not_full.notify() after dequeue; avoid holding lock during expensive work.

  • Timed waits — compute absolute deadline with time.monotonic(); loop with remaining timeout; return False, None, or raise TimeoutError consistently.

  • Shutdown protocol — maintain closed flag under the same lock; wake all waiters with notify_all(); define whether pending items drain or abort.

  • CPU vs I/O concurrency — Python threads help I/O-bound work despite the GIL; CPU-bound image processing usually needs multiprocessing or native extensions.

  • Thread-pool pipeline — use queue.Queue, worker sentinels, join(), and exception collection; bound queue size to apply backpressure and cap memory.

Common pitfalls

Pitfall: Using if queue_empty: wait() instead of while queue_empty: wait() can break under spurious wakeups or competing consumers.

Pitfall: Calling callbacks, image transforms, network I/O, or disk writes while holding the queue lock serializes the system and risks deadlock.

Pitfall: Forgetting shutdown behavior leaves blocked producers or consumers hanging forever; explicitly wake waiters and document drain-vs-cancel semantics.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Featured in interview prep guides

Practice questions

Related concepts

Thread-Safe Queues And Concurrency Primitives — Tech Interview Concept | PracHub