DevPrep

All topics › Multithreading & concurrency

@synchronized

Middle PRO

The essence

@synchronized(obj) { ... } is an Objective-C directive: a critical section that only one thread may enter at a time. Under the hood the runtime keeps a global hash table "object → recursive mutex" and calls objc_sync_enter(obj) / objc_sync_exit(obj) around the block.

Properties:

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