DevPrep

All topics › Multithreading & concurrency

Mutex

Middle PRO

The essence

A mutex (mutual exclusion) is a lock held by exactly one thread at a time. Others that try to acquire it block until unlock(). The key difference from a binary semaphore: a mutex has an owner — only the thread that acquired it may release it, and the kernel can temporarily boost the owner's priority (priority inheritance) so a high-priority waiter does not get stuck behind a low-priority one. That is why "a mutex is a kind of semaphore" is an inaccuracy an interviewer will notice.

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