Daily iOS
Concurrency

Designing a dispatch queue

Design a MyQueue of your own without GCD. Submitting a closure with async(_:) should run it on a background thread pool, it should support serial and concurrent modes, and it should offer sync(_:) too. Can you walk me through the core data structures and how the synchronization works?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The core is a FIFO work queue protected by a lock and a condition variable, plus a fixed-size pool of worker threads. Serial mode uses a running flag so only one item is taken at a time, and sync has the caller wait on a completion semaphore. Run the closure after releasing the lock, and recheck the condition in a while loop.

Share a question