Thread Cancellation

 

The Linux Programming Interface — TLPI
Chapter 32: Thread Cancellation
Master the POSIX thread cancellation mechanism — from sending cancellation requests to handling cleanup safely, covering deferred and asynchronous modes.
7
Topic Pages
12+
Code Examples
40+
Interview Q&A
POSIX
Standard Covered

What is Thread Cancellation?

In a multi-threaded program, threads normally run until they call pthread_exit() or return from their start function. But sometimes you need to forcefully stop a thread from outside — this is called thread cancellation.

Real-world examples: a group of worker threads doing a computation — if one detects a fatal error, it should cancel the rest. Or a GUI app with a “Cancel” button that needs to stop a background worker thread. POSIX Threads provides a clean, safe mechanism for this.

Cancellation Flow Overview
Thread A
(Requestor)
pthread_cancel()
Thread B
(Target)
reaches cancel point
Cleanup
(Handlers Run)
terminates
Thread B
Terminated

Key Concepts in This Chapter
pthread_cancel() Cancellation State Cancellation Type Cancellation Points pthread_testcancel() Cleanup Handlers pthread_cleanup_push() pthread_cleanup_pop() Deferred Cancellation Async Cancellation PTHREAD_CANCELED SUSv3 / SUSv4

All Topics in This Chapter

SECTION 32.1
Canceling a Thread

Learn how to send a cancellation request to another thread using pthread_cancel(). Understand that it only sends the request and does not wait for the thread to stop.

pthread_cancel() API Non-blocking behavior Return values

Read →

SECTION 32.2
Cancellation State and Type

A thread controls whether it accepts cancellation (state) and when it acts on it (type). Covers ENABLE/DISABLE states and DEFERRED/ASYNCHRONOUS types. Also covers fork/exec inheritance rules.

pthread_setcancelstate() pthread_setcanceltype() ENABLE / DISABLE DEFERRED / ASYNC fork/exec inheritance

Read →

SECTION 32.3
Cancellation Points

Deferred cancellation only fires at specific “cancellation points” — special functions listed by SUSv3. Includes the full table of required functions, optional functions, SUSv4 changes, and how PTHREAD_CANCELED is returned via pthread_join().

SUSv3 required list SUSv4 changes PTHREAD_CANCELED Example program

Read →

SECTION 32.4
Testing for Thread Cancellation

Compute-bound threads may never reach a natural cancellation point. pthread_testcancel() manually introduces a cancellation point inside any loop or hot path.

pthread_testcancel() Compute-bound loops Timely cancellation

Read →

SECTION 32.5
Cleanup Handlers

When a thread is canceled mid-way, mutexes may remain locked and memory may leak. Cleanup handlers are functions automatically called on cancellation to restore consistent state. Covers the handler stack, push/pop macros, and the lexical-block pairing rule.

pthread_cleanup_push() pthread_cleanup_pop() Handler stack Macro brace pairing pthread_exit() auto-invoke

Read →

SECTION 32.6
Asynchronous Cancellation

Asynchronous cancellation allows a thread to be stopped at any machine instruction. Explains why this is dangerous, which functions are async-cancel-safe, and the only scenario where it is genuinely safe to use.

PTHREAD_CANCEL_ASYNCHRONOUS Async-cancel-safe functions Resource allocation danger

Read →

SECTION 32.7
Summary & Quick Reference

Full chapter summary, quick-reference API table, key rules to remember, and a complete set of interview questions covering all topics.

API cheat sheet Key rules Interview Q&A

Read →

Ready to start learning?
Begin with Section 32.1 and work through each topic in order for best understanding.

Start: Canceling a Thread → EmbeddedPathashala Home

© EmbeddedPathashala | Free Embedded Systems Education | Based on TLPI Chapter 32

Leave a Reply

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