Context switches in multi-threading
Supporting multitasking is one of the biggest spans in the history of CPU design. In a computer, multitasking refers to running two or more programs at the same time. From the user's point of view, this does not seem complicated or difficult to achieve, but it is indeed a major leap in the history of computer design. In a multitasking system, the CPU needs to handle the operations of all programs, and when the user switches them back and forth, it is necessary to record where those programs are executing. Context switching is a process that allows the CPU to record and restore the state of various running programs so that it can complete the switchover operation.
During context switching, the CPU stops processing the currently running program and saves the specific location where the current program is running so that it can continue running later. From this point of view, context switches are a bit like we are reading several books at the same time, and we need to remember the page numbers that each book is currently reading as we switch back and forth between books. In the program, the "page number" information during context switching is saved in the Process Control block (PCB). PCBs are also often referred to as "switch Frames" (switchframe). The page number information is saved to the CPU's memory until they are used again.
Context switches can occur in three situations: interrupt processing, multitasking, and user-state switching. In interrupt processing, the other program "interrupts" the program that is currently running. When the CPU receives an interrupt request, a context switch is made between the running program and the program initiating the interrupt request. In multitasking, the CPU switches back and forth between the different programs, each with a corresponding processing time slice, and the CPU switching context between the two time slices. For some operating systems, a context switch is also made when a user-state switch is made, although this is not required.
Context switching is supported by the operating system or computer hardware. Some modern operating systems control context switching through the system itself, and the entire switchover process does not rely on hardware support, which allows the operating system to save more context-switching information.
the cost of implementing multithreading
From a single-threaded application to a multithreaded application is not just a benefit, it also has some cost. Don't use multithreading just to use multithreading. It should be clear that multithreading is used when multiple threads can be used more often than the cost of paying. If in doubt, try to measure the performance and responsiveness of your application, not just guesswork.
More complex design
While there are several multithreaded applications that are simpler than single-threaded applications, others are generally more complex. This part of the code requires special attention when accessing shared data in multi-threaded access. The interaction between threads is often very complex. Incorrect thread synchronization produces errors that are very difficult to discover and reproduce to fix.
The cost of context switching
When the CPU switches from executing a thread to executing another thread, it needs to store the local data of the current thread, the program pointer, and so on, then load the local data of the other thread, the program pointer, and so on, before it starts executing. This switchover is called a "context switch". The CPU executes one thread in one context and then switches to another in the context of executing another thread.
Context switching is not cheap. If it is not necessary, you should reduce the occurrence of context switches.
You can read more about context switching through Wikipedia:
Http://en.wikipedia.org/wiki/Context_switch
Increase resource consumption
Threads need to get some resources from the computer while they are running. In addition to the CPU, the thread also needs some memory to maintain its local stack. It also needs to occupy some resources in the operating system to manage threads. We can try to write a program that lets it create 100 threads, and these threads do nothing, just wait and see how much memory the program consumes when it runs.
Reference:
http://ifeve.com/what-is-context-switching/
http://ifeve.com/costs-of-multithreading/
The cost of context switching and multithreading implementation