The article directories
-
- preface
- Context switch
- Does context switching consume resources?
- How can you reduce the additional overhead caused by context switching
preface
I had been offered a handsome reward which I did not cherish. It wasn’t until after I lost it that I realized THAT I could write thread context switches. If a client could give me a chance to reframe my language, I would say three words: “I can write!!”
Context switch
This is context switching.
The CPU executes tasks by allocating time slices, and when one task runs out of time slices, it switches to another task. The state of the previous task will be saved before switching, and the state will be loaded when switching to the task next time.
Does context switching consume resources?
Of course. Let’s look at the switching process in detail:
1Suspends the current task (thread/process) and stores the task's state (context) in the CPU somewhere in memory2Recover a task (thread/process), retrieve the context of the next task in memory and restore it in the CPU register3Jump to the position indicated by the program counter (i.e. jump to the line of code where the task was interrupted) to resume the process in the programCopy the code
Someone has done such a test: take a number in the case of single thread and multi-thread loop count, and count the time.
Through different cycle tests, the rough results are obtained:
We can see that by comparing the data. In the number of cycles below 10,000, the execution speed of series is faster than that of concurrent execution blocks. This is due to the additional overhead of thread context switching.
On Linux, you can use the vmstat command to check the number of context switches (covered in the previous article).
How can you reduce the additional overhead caused by context switching
Reducing the number of context switches can improve the efficiency of multithreading. Reduce context switching by locking concurrent programming, CAS algorithms, avoiding creating too many threads, and using coroutines.
Lockless concurrent programming: when any particular operation is blocked, all cpus can continue processing other operations. To put it another way, in a lockless system, when a given thread is blocked by another thread, all cpus can continue processing other work. The lockless algorithm greatly increases the overall throughput of the system because it only occasionally adds a certain amount of transaction latency. Most high-end database systems are constructed based on lock-free algorithms to meet different levels.
2, the CAS algorithm: Java provides a set of data types of atomic operation (Java. Util. Concurrent. Atomic package), using CAS algorithm to update the data, do not need to be locked. For example, AtomicInteger and AtomicLong.
3. Avoid creating too many threads: If the task volume is small, minimize the number of threads created. In the case of a large number of tasks at a time, we can manage the number of threads through a thread pool to avoid creating multiple threads.
4. Coroutines: Cooperative programs. The idea is that a series of interdependent coroutines use the CPU in turn, with only one coroutine working at a time and the others lying dormant.
It seems that learning coroutine is urgent