Chapter 29: POSIX Threads (Pthreads)

Chapter 29: POSIX Threads (Pthreads)
Complete Tutorial Series — 9 Parts · Beginner to Intermediate
9
Tutorial Parts
18+
Code Examples
45+
Interview Q&A
Book
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.

Series Keywords

POSIX Threads pthread_create() pthread_join() pthread_exit() pthread_detach() pthread_self() pthread_equal() pthread_attr_t Thread IDs Zombie Threads Thread Safety errno Stack Size Detached Thread

All Parts in This Series

Part 1 — Overview of Threads

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.

Thread concept Memory layout Shared vs private resources Threads vs processes Race conditions intro

📄 File: ch29_part1_overview.html

Part 2 — Background Details of the Pthreads API

Pthreads data types, the opaque type rule, per-thread errno, the return value convention (0 = success, positive = error), and the -pthread compiler flag.

pthread_t, pthread_mutex_t … Opaque types Thread-local errno Return value convention gcc -pthread

📄 File: ch29_part2_pthreads_api_background.html

Part 3 — Thread Creation

Full explanation of pthread_create() — all four parameters, the start function contract, argument passing strategies (integer, pointer, struct), and scheduling order guarantees.

pthread_create() signature Start function prototype Passing arguments Struct argument pattern No scheduling guarantee

📄 File: ch29_part3_thread_creation.html

Part 4 — Thread Termination

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().

pthread_exit() return vs exit() Stack safety pthread_cancel() intro Termination comparison table

📄 File: ch29_part4_thread_termination.html

Part 5 — Thread IDs

pthread_self() and pthread_equal(), why you cannot use == for comparison, POSIX TIDs vs Linux kernel TIDs (gettid()), and thread ID reuse after join.

pthread_self() pthread_equal() Opaque type comparison rule POSIX vs kernel TID TID reuse

📄 File: ch29_part5_thread_ids.html

Part 6 — Joining with a Terminated Thread

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().

pthread_join() Thread zombie Return value retrieval Peer model waitpid() comparison

📄 File: ch29_part6_joining_threads.html

Part 7 — Detaching a Thread

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().

pthread_detach() Joinable vs detached Self-detach PTHREAD_CREATE_DETACHED Fire-and-forget pattern

📄 File: ch29_part7_detaching_threads.html

Part 8 — Thread Attributes

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.

pthread_attr_t pthread_attr_init/destroy Stack size control Scheduling attributes PTHREAD_STACK_MIN

📄 File: ch29_part8_thread_attributes.html

Part 9 — Threads vs Processes & Chapter Summary

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.

Threads vs processes trade-offs Thread safety Fault isolation Chapter summary Exercises 29-1, 29-2

📄 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

Start Learning

Begin with Part 1 or jump to the topic you need most.

EmbeddedPathashala Home Chapter 30: Mutexes →

Leave a Reply

Your email address will not be published. Required fields are marked *