什么是“线程"?(真的)? [英] What is a "thread" (really)?

查看:19
本文介绍了什么是“线程"?(真的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找一个好的定义,并理解什么是线程.

I have been trying to find a good definition, and get an understanding, of what a thread really is.

似乎我肯定遗漏了一些明显的东西,但每次我读到什么是线程时,它几乎是一个循环定义,一个线程是一个执行线程"或一种划分运行任务的方法"".呃呃.嗯?

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?

从我所读到的内容看来,线程并不是真正的东西,就像进程一样.它实际上只是一个概念.根据我对其工作方式的理解,处理器为程序执行一些命令(它被称为执行线程),然后当它需要切换到其他程序的处理时位,它将当前正在执行的程序的状态存储在某处(线程本地存储),然后开始执行其他程序的指令.和来回.这样,线程实际上只是当前正在运行的程序的执行路径之一"的概念.

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

来自维基百科:

计算机科学中的线程是执行线程的缩写.线程是程序将自身划分(称为拆分")为两个或多个同时(或伪同时)运行的任务的方式.线程和进程一个操作系统与另一个操作系统不同,但一般来说,一个线程包含在一个进程中,同一进程中的不同线程共享相同的资源,而同一多任务操作系统中的不同进程则没有."

"A thread in computer science is short for a thread of execution. Threads are a way for a program to divide (termed "split") itself into two or more simultaneously (or pseudo-simultaneously) running tasks. Threads and processes differ from one operating system to another but, in general, a thread is contained inside a process and different threads in the same process share same resources while different processes in the same multitasking operating system do not."

我说的对吗?错误的?什么是线程?

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.

线程是受控制流(函数调用、循环、goto)影响的软件单元,因为这些指令在指令指针上运行,并且属于特定线程.线程通常根据某种优先级方案进行调度(尽管可以设计一个每个处理器内核一个线程的系统,在这种情况下,每个线程始终在运行,不需要调度).

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

这篇关于什么是“线程"?(真的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆