Concurrency in C: POSIX threads
You run work in parallel with pthreads, prevent race conditions and deadlocks, and use atomics and thread pools correctly.
Level 401 · 17 lessons · 15 practice labs
Lessons
Threads
- Threads versus processes: shared memory, separate stacks
- pthread_create and pthread_join
- Passing data to threads and getting results back
- Thread-local data: _Thread_local
- Short task: sum a large array in parallel
Synchronisation
- Race conditions: making the bug visible
- Mutual exclusion: pthread_mutex
- Deadlock: causes and lock ordering
- Condition variables and producer-consumer
- Read-write locks and semaphores
- Short task: a bounded, thread-safe queue
Atomics and tooling
- C11 atomics and memory ordering
- A lock-free counter and the ABA problem
- Finding races with ThreadSanitizer and Helgrind
- Designing a thread pool
- Short task: find, fix and prove the race fix
Capstone
- Capstone: a thread-pooled file hasher
Practice labs
- Finding an array maximum in parallel: Write a program that splits an array over four threads and gathers each one's maximum with pthread_join.
- Fixing the lifetime of a thread argument: Fix code that passes the loop variable's address to threads and gives random wrong results.
- A thread-local error buffer: Make a global error message buffer _Thread_local and test that threads no longer overwrite each other's messages.
- Protecting a shared total with a mutex: Add a mutex to a payment total incremented by several threads and show the result is the same every run.
- Preventing deadlock with lock ordering: Fix code that deadlocks on transfers between two accounts by using a consistent lock order.
- Taking resources with trylock and backoff: Run workers that must lock three resources at once without deadlock using trylock and random backoff.
- A ready signal with a condition variable: Write code where workers wait until settings are loaded with a condition variable instead of a busy loop.
- Waiting that survives spurious wake-ups: Change a queue that uses pthread_cond_wait with if into a while loop so the stress test never reads an empty queue.
- Guarding a read-heavy table with an rwlock: Guard a routing table that is read often and updated rarely with pthread_rwlock instead of a mutex and compare timings.
- Shutting down a thread pool cleanly: Write a shutdown function that finishes the queued jobs and ends all workers without leaks.
- Keeping statistics with a C11 atomic counter: Keep a request counter with atomic_fetch_add instead of a mutex and show it is correct under a stress test.
- Finding a hidden race with TSan: Find and fix the race ThreadSanitizer reports in a cache whose tests usually pass.
- Publishing data with release and acquire: Fix producer-consumer code that sets a flag with relaxed ordering using release/acquire and explain why in a comment.
- Repairing a lock bug from a Helgrind report: Interpret Helgrind's lock-order violation report, fix the code and show the report is clean.
- Guarding a settings struct with a reader lock: Protect a settings struct read by many threads and occasionally updated by one with pthread_rwlock and show by test that no torn reads remain.
About this course
You run work in parallel with pthreads, prevent race conditions and deadlocks, and use atomics and thread pools correctly.
What you will learn
- Threads
- Synchronisation
- Atomics and tooling
- Capstone
Prerequisites
You should have practised the topics of the same path up to level 301.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 401; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.