Daily iOS
Concurrency

Multi-reader single-writer queue

You want a property that several threads can read at the same time while writes happen one at a time. Back in the Objective-C days there was a pattern of dispatch_sync in the getter and dispatch_barrier_async in the setter, and the interviewer asks: "If the getter is sync, that's synchronous, so how can several reads happen at once?" How would you explain that?

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

Because the queue is concurrent. sync means the caller waits for the block to finish, not that the block is serialized with other blocks on the queue. Read blocks run in parallel with each other, and only a write submitted with barrier runs alone after everything before it has finished. barrier only works on a custom concurrent queue.

Share a question