TRT
Sign in

Advanced · Module 7 · Lesson 5 of 8

Thread-Specific Data and Thread-Local Storage

Isolate global state across threads using POSIX pthread_key APIs and C11 _Thread_local, trace %fs segment register addressing, and manage thread cleanup destructors.

Pro — members onlyAbout 16 minutes

By the end you can

  • Eliminate global mutex contention by binding private state to threads using pthread keys
  • Automate dynamic resource reclamation using pthread_key_create destructor callbacks
  • Implement high-performance zero-overhead storage using C11 _Thread_local and compiler TLS
  • Trace low-level Thread Control Block addressing via the %fs segment register on x86-64

Builds on Read-Write Locks and Barrier Synchronization.

01 · A question first

How can the C library provide a global variable like errno that every thread can modify independently without locks or race conditions? The secret lies in thread-local storage and CPU segment registers.

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 problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem