DevPrep

All topics › Multithreading & concurrency

What is a barrier and on which queue does it work?

Middle PRO

The essence

A barrier is a task flag on your own concurrent queue that makes the task exclusive: the barrier block starts only after every task submitted before it has finished, and tasks submitted after it wait until it completes. Effectively the queue becomes serial for the duration of the barrier. On global queues the flag is ignored — they are shared by the process, and the block runs as a plain async. On a serial queue a barrier is pointless: everything is exclusive anyway.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store