TRT
Sign in

Advanced · Module 11 · Lesson 2 of 6

Mutual Exclusion: mutex, lock_guard and unique_lock

Protect shared counters and caches with mutex ownership, compare lock_guard with unique_lock, and scale reads with shared_mutex.

Pro — members onlyAbout 10 minutes

By the end you can

  • Protect shared data with mutex and lock_guard correctly
  • Compare lock_guard with unique_lock for manual control
  • Scale a DNS cache with shared_mutex for parallel readers

Builds on Launching and Joining Threads.

01 · A question first

Two threads increment one counter ten thousand times each, yet the total prints below twenty thousand. A DNS cache shows the same flaw when writes collide with reads. Which lock makes the count exact, and how can many readers proceed together while writers still hold exclusive access?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the race
  • How the idea works
  • Why lock choice matters
  • Readers in parallel
  • Pitfalls and variations
  • Where it is used and what comes next

03 · Practice

Something wrong with this lesson? Report a problem