问题描述
我一直在努力寻找一个好的定义,并了解线程的真正含义.
I have been trying to find a good definition, and get an understanding, of what a thread really is.
似乎我必须缺少一些明显的东西,但是每次我读到什么是线程时,它几乎都是一个循环定义,例如一个线程是一个执行线程"或一种划分为正在运行的任务的方法" ". h嗯?
It seems that I must be missing something obvious, but every time I read about what a thread is, it's almost a circular definition, a la "a thread is a thread of execution" or " a way to divide into running tasks". Uh uh. Huh?
从我读到的内容来看,线程似乎不是一个真正的具体东西,就像一个进程一样.实际上,这只是一个概念.根据我对这种工作方式的了解,处理器会为某个程序执行一些命令(已被称为执行线程),然后在需要切换至其他程序处理时,处理器才能执行该命令.位,它将当前正在执行的程序的 state 存储在某个地方(线程本地存储),然后开始执行其他程序的指令.来回.这样,线程实际上只是当前正在运行的程序的执行路径之一"的概念.
It seems from what I have read that a thread is not really something concrete, like a process is. It is in fact just a concept. From what I understand of the way this works, a processor executes some commands for a program (which has been termed a thread of execution), then when it needs to switch to processing for some other program for a bit, it stores the state of the program it's currently executing for somewhere (Thread Local Storage) and then starts executing the other program's instructions. And back and forth. Such that, a thread is really just a concept for "one of the paths of execution" of a program that is currently running.
与过程不同,它的确是东西-它是资源的集合等.
Unlike a process, which really is something - it is a conglomeration of resources, etc.
作为一个定义的示例,它对我并没有太大帮助. .
As an example of a definition that didn't really help me much . . .
来自维基百科:
所以我说的对吗?错误的?真正的线程是什么?
So am I right? Wrong? What is a thread really?
显然,线程也被赋予了自己的调用堆栈,因此这在某种程度上是具体的事物.
Apparently a thread is also given its own call stack, so that is somewhat of a concrete thing.
推荐答案
线程是处理器寄存器(用于单个内核)的一组独立的值.由于其中包括指令指针(又称为程序计数器),因此它可以控制以什么顺序执行的内容.它还包括堆栈指针,堆栈指针最好为每个线程指向唯一的内存区域,否则它们将相互干扰.
A thread is an independent set of values for the processor registers (for a single core). Since this includes the Instruction Pointer (aka Program Counter), it controls what executes in what order. It also includes the Stack Pointer, which had better point to a unique area of memory for each thread or else they will interfere with each other.
线程是受控制流(函数调用,循环,转到)影响的软件单元,因为这些指令在指令指针上操作,并且属于特定线程.线程通常是根据某种优先级分配方案进行调度的(尽管可以设计每个处理器内核只有一个线程的系统,在这种情况下,每个线程始终在运行,不需要调度).
Threads are the software unit affected by control flow (function call, loop, goto), because those instructions operate on the Instruction Pointer, and that belongs to a particular thread. Threads are often scheduled according to some prioritization scheme (although it's possible to design a system with one thread per processor core, in which case every thread is always running and no scheduling is needed).
实际上,指令指针的值和存储在该位置的指令足以确定该指令指针的新值.对于大多数指令,这只是将IP扩大了指令的大小,但是控制流指令以其他可预测的方式更改了IP. IP所采用的值的顺序形成了一条贯穿程序代码的执行路径,从而产生了线程"这个名称.
In fact the value of the Instruction Pointer and the instruction stored at that location is sufficient to determine a new value for the Instruction Pointer. For most instructions, this simply advances the IP by the size of the instruction, but control flow instructions change the IP in other, predictable ways. The sequence of values the IP takes on forms a path of execution weaving through the program code, giving rise to the name "thread".
这篇关于什么是“线程"? (真的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!