ThreadPool C # context switching logic

I tested the code that ThreadPool uses in C #, and I noticed that a lot of unnecessary context switching is happening. Although one thread performs regular statements, it switches to context. They seem to literally go back and forth along each line. I was wondering what kind of logic it is. Why will the thread be disabled for regular performances?

+4
source share
2 answers

Threads must be executed at the same time, simultaneously starting both threads at the "same time". In reality, each thread (on one processor computer) receives a finite period of time (called a quantum), which is performed before the context switches. This, of course, is a gross simplification, but basically this is what happens.

When you start both threads in the debugger and execute it (which I suppose you do), the jump action for each of the statements causes it to exceed the time that the thread has to execute it, and you get the context switches back to another thread.

+4
source

When executing code during debugging, each line takes WAY longer than during normal execution. This is why there are so many context switches. Usually they occur several times per second.

+2
source

Source: https://habr.com/ru/post/1341122/


All Articles