Tutorial Parts
Code Examples
Interview Q&A
TLPI Ch 29
This series covers POSIX Threads (Pthreads) from the ground up, based on Chapter 29 of The Linux Programming Interface. Each part is a self-contained tutorial with theory, inline diagrams, C code examples, and interview questions. Parts are designed for students and engineers beginning with multithreaded Linux programming.
All Parts in This Series
What threads are, how they share memory (with layout diagram), the difference between concurrency and parallelism, and why threads are preferred over processes in many scenarios.
📄 File: ch29_part1_overview.html
Pthreads data types, the opaque type rule, per-thread errno, the return value convention (0 = success, positive = error), and the -pthread compiler flag.
📄 File: ch29_part2_pthreads_api_background.html
Full explanation of pthread_create() — all four parameters, the start function contract, argument passing strategies (integer, pointer, struct), and scheduling order guarantees.
📄 File: ch29_part3_thread_creation.html
The four ways a thread ends: return, pthread_exit(), pthread_cancel(), and process-wide exit(). The stack pointer danger and the special behaviour when main calls pthread_exit().
📄 File: ch29_part4_thread_termination.html
pthread_self() and pthread_equal(), why you cannot use == for comparison, POSIX TIDs vs Linux kernel TIDs (gettid()), and thread ID reuse after join.
📄 File: ch29_part5_thread_ids.html
pthread_join() in detail — how it blocks, collecting the return value, zombie threads and their dangers, the peer model vs process hierarchy, and differences from waitpid().
📄 File: ch29_part6_joining_threads.html
Joinable vs detached states, pthread_detach(), self-detach pattern, creating detached threads at birth using PTHREAD_CREATE_DETACHED, and rules about exit() vs pthread_exit().
📄 File: ch29_part7_detaching_threads.html
The pthread_attr_t object — initialise → set → create → destroy workflow, all common attributes (stack size, detach state, scheduling), why and when to change stack size, and getter/setter functions.
📄 File: ch29_part8_thread_attributes.html
Full advantages and disadvantages comparison, practical decision guide, complete chapter summary with all key points, textbook exercises 29-1 and 29-2 with detailed analysis and solutions.
📄 File: ch29_part9_threads_vs_processes_summary.html
Quick Reference — Key Functions
| Function | Purpose | Returns |
|---|---|---|
| pthread_create() | Create a new thread | 0 / error |
| pthread_exit() | Terminate calling thread | void (no return) |
| pthread_join() | Wait for thread + cleanup | 0 / error |
| pthread_detach() | Mark thread for auto-cleanup | 0 / error |
| pthread_self() | Get calling thread’s ID | pthread_t |
| pthread_equal() | Compare two thread IDs | non-zero if equal |
| pthread_attr_init() | Init thread attributes object | 0 / error |
| pthread_attr_destroy() | Free attributes object | 0 / error |
| pthread_attr_setdetachstate() | Set joinable/detached | 0 / error |
| pthread_attr_setstacksize() | Set thread stack size | 0 / error |
Begin with Part 1 or jump to the topic you need most.
