notesonly.in

One notebook for every subject — open it anywhere.

Log in

Multithreading basics

Object-Oriented Programming · Engineering

Study notes

Two threads increment a shared counter: both read 5, both write 6, losing an update: race condition. Fix with a lock: thread acquires mutex, increments, releases; the other waits. But two locks acquired in opposite order by two threads causes deadlock: each waits forever. Use lock ordering or timeouts to prevent.

← Back to topics for Engineering