Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Time-Slice, Context-Switch and Thread interference

As a beginner in multi-threading, I struggle a little bit with these terms. Can someone help me make a border between them? I am afraid not to learn something wrong at the beginning and I have no one to 'test' me.

Please correct me if I am wrong :)

If two threads run at time on 1 CPU core, they would be context-switched. Context-switching is based on time-slice algorithm, that helps Scheduler to 'decide' which one and how long to keep on core. It doesn't matter if those 2 threads share same variable to the these terms, right?

But then there is thread interference. This term is based only when two threads share same variable?

Am I any close to saying it correct?

like image 989
Stefan Avatar asked Aug 07 '26 11:08

Stefan


1 Answers

"context," in a nutshell, is the collection of values that need to be loaded into the Program Counter register, the Stack Pointer register, and other registers of a CPU in order to make it start or resume execution of a thread.

"Scheduler" is the part of the operating system that decides which thread(s) should run on which CPUs and when.

"context switch" is what we call it when the scheduler saves the context of one thread, and installs the context of some other thread on the same CPU, and lets it run.

"Preemption" is what we call it when the OS switches out some thread for some reason that is not a reaction to something that the thread just did.

"time slice" is the period of time that the scheduler grants to each newly (re)started thread before the scheduler will preempt it in order to let some other waiting thread run.

Finally, (I'm guessing) When you read, "Interference," that probably referred to anything that one thread does which, because of some defect in the program, interferes with the function of some other thread. (E.g., by changing the value of some shared variable, at a time when the other thread was depending on the variable to not change.)

like image 78
Solomon Slow Avatar answered Aug 09 '26 00:08

Solomon Slow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!