Time-Slice, Context-Switch和Thread interference之间的区别

huangapple go评论70阅读模式
英文:

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

问题

作为在“多线程编程”方面的初学者,我对这些“术语”有些困惑。是否有人可以帮我将它们区分开来?我担心在刚开始学习时学错东西,而且没有人可以“测试”我。

如果两个“线程”同时在“1个CPU核心”上运行,它们将会发生“上下文切换”。 “上下文切换”基于“时间片算法”,这有助于“调度器”决定在“核心”上保持哪一个线程以及持续多长时间。这两个“线程”如果共享同一个“变量”,这些术语是否无关紧要,对吗?

但随后会出现“线程干扰”。这个术语仅在两个线程共享同一个“变量”时才会出现,对吗?

我离正确表达还差得远吗?

英文:

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 Time-Slice, Context-Switch和Thread interference之间的区别

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?

答案1

得分: 4

"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 to start or resume the 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 when the scheduler saves the context of one thread, and installs the context of another thread on the same CPU, allowing it to run.

"Preemption" is when the OS switches out a thread for a reason not directly caused by the thread's actions.

"time slice" is the period the scheduler allows a newly (re)started thread to run before preempting it to let another waiting thread run.

Finally, "Interference" likely refers to actions of one thread that, due to program defects, disrupt the function of another thread. For example, modifying a "shared variable" when the other thread relies on it to remain unchanged.

英文:

"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.)

huangapple
  • 本文由 发表于 2020年9月16日 10:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63912452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定