DevPrep

All topics › Multithreading & concurrency

Semaphore

Middle PRO

The essence

A semaphore is a synchronization primitive with a counter. wait() decrements the counter and, if it went negative, blocks the thread until someone calls signal() (which increments the counter and wakes one waiter). A semaphore with value N lets at most N threads into the section at once; with an initial value of 0 it acts as an "event signal".

iOS has three API families:

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