Anatomy of a Context Switch: What the Kernel Does Between Two Threads
A context switch looks free until you profile a thread-heavy workload and watch it vanish into the kernel. Here is everything the scheduler actually does to swap one thread for another.