Context Switches between Threads

When we have one CPU and 2 or more threads in a process, the OS will sooner or later perform a context switch between threads. The data of the currently executing thread will be copied from the CPU back to memory, while the data of a thread-to-be-run will be copied thereafter from memory to the CPU to run on it.

The above scenario is quite like the one we saw for processes. The difference is that intra-process context switches (that is, those that happen within a process, a.k.a. between threads) are faster than inter-process context switches (those that happen between processes.)

The reason is that, when we switch between one process to its friend, we also need to flush (= save recent changes in) the cache to memory, which is a time-expensive action. Nonetheless, when we switch between threads, there is no need in flushing the cache since the threads of the same process share the same memory and hence the same cache.