multithreading 什么是“线程”(真的)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5201852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a "thread" (really)?
提问by richard
I have been trying to find a good definition, and get an understanding, of what a threadreally 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 somethingconcrete, 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 stateof 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 . . .
作为一个对我没有多大帮助的定义的例子。. .
From Wikipedia:
来自维基百科:
"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?
所以我说得对吗?错误的?什么是线程?
Edit:Apparently a thread is also given its own call stack, so that is somewhat of a concrete thing.
编辑:显然,一个线程也给出了自己的调用堆栈,所以这是有点的具体事情。
采纳答案by Ben Voigt
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).
线程是受控制流(函数调用、循环、goto)影响的软件单元,因为这些指令对指令指针进行操作,并且属于特定线程。线程通常根据某种优先级方案进行调度(尽管可以设计一个每个处理器内核一个线程的系统,在这种情况下,每个线程始终在运行并且不需要调度)。
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".
事实上,指令指针的值和存储在该位置的指令足以确定指令指针的新值。对于大多数指令,这只是按照指令的大小来提高 IP,但控制流指令会以其他可预测的方式更改 IP。IP 所采用的值序列形成了一条贯穿程序代码的执行路径,从而产生了“线程”这个名称。
回答by pwnall
A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.
线程是一个执行上下文,它是 CPU 执行指令流所需的所有信息。
Suppose you're reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.
假设您正在阅读一本书,并且您现在想休息一下,但您希望能够回来并从停止的确切位置继续阅读。实现这一目标的一种方法是记下页码、行号和字号。因此,您阅读一本书的执行上下文是这 3 个数字。
If you have a roommate, and she's using the same technique, she can take the book while you're not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.
如果你有一个室友,而她也在使用同样的技巧,她可以在你不使用的时候拿起这本书,然后从她停下的地方继续阅读。然后你可以把它拿回来,从你原来的地方恢复它。
Threads work in the same way. A CPU is giving you the illusion that it's doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.
线程以相同的方式工作。CPU 会给您一种错觉,即它正在同时进行多项计算。它通过在每次计算上花费一些时间来做到这一点。它可以做到这一点,因为它对每个计算都有一个执行上下文。就像您可以与朋友共享一本书一样,许多任务可以共享一个 CPU。
On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.
在技术层面上,执行上下文(因此是线程)由 CPU 寄存器的值组成。
Last: threads are different from processes. A thread is a context of execution, while a process is a bunch of resources associated with a computation. A process can have one or many threads.
最后:线程不同于进程。线程是执行的上下文,而进程是与计算相关的一堆资源。一个进程可以有一个或多个线程。
Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).
说明:与进程关联的资源包括内存页(进程中的所有线程都具有相同的内存视图)、文件描述符(例如,打开的套接字)和安全凭证(例如,启动该进程的用户的 ID)。过程)。
回答by Leos313
In order to define a thread formally, we must first understand the boundaries of where a thread operates.
为了正式定义线程,我们必须首先了解线程操作的边界。
A computer program becomes a processwhen it is loaded from some store into the computer's memory and begins execution. A process can be executed by a processor or a set of processors. A process description in memory contains vital information such as the program counter which keeps track of the current position in the program (i.e. which instruction is currently being executed), registers, variable stores, file handles, signals, and so forth.
当计算机程序从某个存储区加载到计算机内存中并开始执行时,它就变成了一个进程。一个进程可以由一个处理器或一组处理器执行。内存中的进程描述包含重要信息,例如跟踪程序中当前位置的程序计数器(即当前正在执行的指令)、寄存器、变量存储、文件句柄、信号等。
A threadis a sequence of such instructions within a program that can be executed independently of other code. The figure shows the concept:
甲螺纹是可以独立于其它代码执行的程序内的这样的指令序列。下图展示了这个概念:
Threads are within the same process address space, thus, much of the information present in the memory description of the process can be shared across threads.
线程位于相同的进程地址空间内,因此,进程的内存描述中存在的大部分信息可以跨线程共享。
Some information cannot be replicated, such as the stack (stack pointer to a different memory area per thread), registers and thread-specific data. This information suffices to allow threads to be scheduled independentlyof the program's main thread and possibly one or more other threads within the program.
某些信息无法复制,例如堆栈(每个线程指向不同内存区域的堆栈指针)、寄存器和线程特定数据。这一信息足以允许独立于程序的主线程和程序内的一个或多个其他线程来调度线程。
Explicit operating system support is required to run multithreaded programs. Fortunately, most modern operating systems support threads such as Linux (via NPTL), BSD variants, Mac OS X, Windows, Solaris, AIX, HP-UX, etc. Operating systems may use different mechanisms to implement multithreading support.
运行多线程程序需要显式的操作系统支持。幸运的是,大多数现代操作系统都支持线程,例如 Linux(通过 NPTL)、BSD 变体、Mac OS X、Windows、Solaris、AIX、HP-UX 等。操作系统可能使用不同的机制来实现多线程支持。
Here, you can find more information about the topic. That was also my information-source.
在这里,您可以找到有关该主题的更多信息。那也是我的信息来源。
Let me just add a sentence coming from Introduction to Embedded Systemby Edward Leeand Seshia:
让我补充从哪里来一句话介绍嵌入式系统由爱德华·李和Seshia:
Threads are imperative programs that run concurrently and share a memory space. They can access each others' variables. Many practitioners in the field use the term “threads” more narrowly to refer to particular ways of constructing programs that share memory, [others] to broadly refer to any mechanism where imperative programs run concurrently and share memory. In this broad sense, threads exist in the form of interrupts on almost all microprocessors, even without any operating system at all (bare iron).
线程是并发运行并共享内存空间的命令式程序。他们可以访问彼此的变量。该领域的许多从业者更狭义地使用术语“线程”来指代构建共享内存的程序的特定方式,[其他人] 泛指任何命令式程序并发运行并共享内存的机制。从广义上讲,线程以中断的形式存在于几乎所有微处理器上,甚至根本没有任何操作系统(裸机)。
回答by Joey Adams
Processes are like two people using two different computers, who use the network to share data when necessary. Threads are like two people using the same computer, who don't have to share data explicitly but must carefully take turns.
流程就像两个人使用两台不同的计算机,在必要时使用网络共享数据。线程就像两个人使用同一台计算机,他们不必明确共享数据,但必须小心地轮流。
Conceptually, threads are just multiple worker bees buzzing around in the same address space. Each thread has its own stack, its own program counter, etc., but all threads in a process share the same memory. Imagine two programs running at the same time, but they both can access the same objects.
从概念上讲,线程只是在同一地址空间中嗡嗡作响的多个工蜂。每个线程都有自己的堆栈、自己的程序计数器等,但一个进程中的所有线程共享相同的内存。想象两个程序同时运行,但它们都可以访问相同的对象。
Contrast this with processes. Processes each have their own address space, meaning a pointer in one process cannot be used to refer to an object in another (unless you use shared memory).
将此与流程进行对比。每个进程都有自己的地址空间,这意味着一个进程中的指针不能用于引用另一个进程中的对象(除非您使用共享内存)。
I guess the key things to understand are:
我想要理解的关键是:
- Both processes and threads can "run at the same time".
- Processes do not share memory (by default), but threads share all of their memory with other threads in the same process.
- Each thread in a process has its own stack and its own instruction pointer.
- 进程和线程都可以“同时运行”。
- 进程不共享内存(默认情况下),但线程与同一进程中的其他线程共享所有内存。
- 进程中的每个线程都有自己的堆栈和自己的指令指针。
回答by chatuur
I am going to use a lot of text from the book Operating Systems Concepts by ABRAHAM SILBERSCHATZ, PETER BAER GALVIN and GREG GAGNE along with my own understanding of things.
我将使用 ABRAHAM SILBERSCHATZ、PETER BAER GALVIN 和 GREG GAGNE 合着的《操作系统概念》一书中的大量文字以及我自己对事物的理解。
Process
过程
Any application resides in the computer in the form of text (or code).
任何应用程序都以文本(或代码)的形式存在于计算机中。
We emphasize that a program by itself is not a process. A program is a passive entity, such as a file containing a list of instructions stored on disk (often called an executable file).
我们强调程序本身并不是一个过程。程序是一个被动实体,例如包含存储在磁盘上的指令列表的文件(通常称为可执行文件)。
When we start an application, we create an instance of execution. This instance of execution is called a process. EDIT:(As per my interpretation, analogous to a class and an instance of a class, the instance of a class being a process. )
当我们启动一个应用程序时,我们创建了一个执行实例。这种执行实例称为进程。编辑:(根据我的解释,类似于一个类和一个类的实例,一个类的实例是一个进程。)
An example of processes is that of Google Chrome. When we start Google Chrome, 3 processes are spawned:
进程的一个例子是谷歌浏览器的进程。当我们启动谷歌浏览器时,会产生 3 个进程:
? The browserprocess is responsible for managing the user interface as well as disk and network I/O. A new browser process is created when Chrome is started. Only one browser process is created.
? Rendererprocesses contain logic for rendering web pages. Thus, they contain the logic for handling HTML, Javascript, images, and so forth. As a general rule, a new renderer process is created for each website opened in a new tab, and so several renderer processes may be active at the same time.
? A plug-inprocess is created for each type of plug-in (such as Flash or QuickTime) in use. Plug-in processes contain the code for the plug-in as well as additional code that enables the plug-in to communicate with associated renderer processes and the browser process.
? 所述浏览器进程是负责管理的用户界面,以及磁盘和网络I / O。Chrome 启动时会创建一个新的浏览器进程。只创建一个浏览器进程。
? 渲染器进程包含用于渲染网页的逻辑。因此,它们包含处理 HTML、Javascript、图像等的逻辑。通常,会为在新选项卡中打开的每个网站创建一个新的渲染器进程,因此多个渲染器进程可能同时处于活动状态。
? 为使用中的每种类型的插件(例如 Flash 或 QuickTime)创建一个插件进程。插件进程包含插件的代码以及使插件能够与关联的呈现器进程和浏览器进程通信的附加代码。
Thread
线
To answer this I think you should first know what a processor is. A Processor is the piece of hardware that actually performs the computations. EDIT: (Computations like adding two numbers, sorting an array, basically executing the code that has been written)
要回答这个问题,我认为您首先应该知道处理器是什么。处理器是实际执行计算的硬件。编辑:(计算如将两个数字相加,对数组进行排序,基本上执行已编写的代码)
Now moving on to the definition of a thread.
现在转到线程的定义。
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.
线程是CPU 利用率的基本单位;它包括线程 ID、程序计数器、寄存器集和堆栈。
EDIT: Definition of a thread from intel's website:
编辑:来自英特尔网站的线程定义:
A Thread, or thread of execution, is a software term for the basic ordered sequence of instructions that can be passed through or processed by a single CPU core.
线程或执行线程是一个软件术语,表示可以通过单个 CPU 内核或由单个 CPU 内核处理的基本有序指令序列。
So, if the Renderer process from the Chrome application sorts an array of numbers, the sorting will take place on a thread/thread of execution. (The grammar regarding threads seems confusing to me)
因此,如果来自 Chrome 应用程序的 Renderer 进程对数字数组进行排序,则排序将在一个线程/执行线程上进行。(关于线程的语法对我来说似乎很混乱)
My Interpretation of Things
我对事物的解释
A process is an execution instance. Threads are the actual workers that perform the computations via CPU access. When there are multiple threads running for a process, the process provides common memory.
进程是一个执行实例。线程是通过 CPU 访问执行计算的实际工作人员。当一个进程有多个线程在运行时,该进程提供公共内存。
EDIT: Other Information that I found useful to give more context
编辑: 我发现有助于提供更多上下文的其他信息
All modern day computer have more than one threads. The number of threads in a computer depends on the number of cores in a computer.
所有现代计算机都有多个线程。计算机中的线程数取决于计算机中的内核数。
Concurrent Computing:
并发计算:
From Wikipedia:
来自维基百科:
Concurrent computing is a form of computing in which several computations are executed during overlapping time periods—concurrently—instead of sequentially (one completing before the next starts). This is a property of a system—this may be an individual program, a computer, or a network—and there is a separate execution point or "thread of control" for each computation ("process").
并发计算是一种计算形式,其中多个计算在重叠的时间段内(同时)执行,而不是按顺序(一个在下一个开始之前完成)。这是一个系统的属性——这可能是一个单独的程序、一台计算机或一个网络——并且每个计算(“进程”)都有一个单独的执行点或“控制线程”。
So, I could write a program which calculates the sum of 4 numbers:
所以,我可以编写一个程序来计算 4 个数字的总和:
(1 + 3) + (4 + 5)
In the program to compute this sum (which will be one process running on a thread of execution) I can fork another process which can run on a different thread to compute (4 + 5) and return the result to the original process, while the original process calculates the sum of (1 + 3).
在计算这个总和的程序中(这将是一个在执行线程上运行的进程),我可以派生另一个可以在不同线程上运行的进程来计算 (4 + 5) 并将结果返回给原始进程,而原始过程计算 (1 + 3) 的总和。
回答by Orbit
Unfortunately, threads do exist. A thread is something tangible. You can kill one, and the others will still be running. You can spawn new threads.... although each thread is not it's own process, they are running separately inside the process. On multi-core machines, 2 threads could run at the same time.
不幸的是,线程确实存在。线是有形的东西。你可以杀死一个,其他的仍然会运行。您可以生成新线程.... 虽然每个线程不是它自己的进程,但它们在进程内单独运行。在多核机器上,2 个线程可以同时运行。
http://en.wikipedia.org/wiki/Simultaneous_multithreading
http://en.wikipedia.org/wiki/Simultaneous_multithreading
回答by Valentin Radu
A thread is nothing more than a memory context (or how Tanenbaum better puts it, resource grouping) with execution rules. It's a software construct. The CPU has no idea what a thread is (some exceptions here, some processors have hardware threads), it just executes instructions.
一个线程只不过是一个带有执行规则的内存上下文(或者 Tanenbaum 如何更好地放置它,资源分组)。这是一个软件结构。CPU 不知道线程是什么(这里有些例外,有些处理器有硬件线程),它只是执行指令。
The kernel introduces the thread and process concept to manage the memory and instructions order in a meaningful way.
内核引入了线程和进程的概念,以有意义的方式管理内存和指令顺序。
回答by hobbs
The answer varies hugelyacross different systems and different implementations, but the most important parts are:
答案在不同的系统和不同的实现中差异很大,但最重要的部分是:
- A thread has an independent thread of execution (i.e. you can context-switch away from it, and then back, and it will resume running where it was).
- A thread has a lifetime (it can be created by another thread, and another thread can wait for it to finish).
- It probably has less baggage attached than a "process".
- 一个线程有一个独立的执行线程(即你可以从它那里进行上下文切换,然后返回,它会在原来的地方继续运行)。
- 一个线程有一个生命周期(它可以由另一个线程创建,另一个线程可以等待它完成)。
- 它可能比“过程”附加的包袱更少。
Beyond that: threads could be implemented within a single process by a language runtime, threads could be coroutines, threads could be implemented within a single process by a threading library, or threads could be a kernel construct.
除此之外:线程可以由语言运行时在单个进程中实现,线程可以是协程,线程可以通过线程库在单个进程中实现,或者线程可以是内核构造。
In several modern Unix systems, including Linux which I'm most familiar with, everythingis threads -- a process is merely a typeof thread that shares relatively few things with its parent (i.e. it gets its own memory mappings, its own file table and permissions, etc.) Reading man 2 clone
, especially the list of flags, is really instructive here.
在一些现代的Unix系统,包括Linux其中我最熟悉的,一切是线程-这个过程仅仅是一个类型的线程共享与其父相对较少的东西(即它都有自己的内存映射,它自己的文件表和权限等)阅读man 2 clone
,尤其是标志列表,在这里真的很有启发性。
回答by Flipper
This was taken from a Yahoo Answer:
这是从雅虎回答中获取的:
A thread is a coding construct unaffect by the architecture of an application. A single process frequently may contain multiple threads. Threads can also directly communicate with each other since they share the same variables.
Processes are independent execution units with their own state information. They also use their own address spaces and can only interact with other processes through interprocess communication mechanisms.
线程是不受应用程序架构影响的编码结构。单个进程经常可能包含多个线程。线程也可以直接相互通信,因为它们共享相同的变量。
进程是具有自己状态信息的独立执行单元。它们也使用自己的地址空间,只能通过进程间通信机制与其他进程交互。
However, to put in simpler terms threads are like different "tasks". So think of when you are doing something, for instance you are writing down a formula on one paper. That can be considered one thread. Then another thread is you writing something else on another piece of paper. That is where multitasking comes in.
然而,简单地说,线程就像不同的“任务”。所以想想当你在做某事时,例如你在一张纸上写下一个公式。这可以被认为是一个线程。然后另一个线程是你在另一张纸上写其他东西。这就是多任务处理的用武之地。
Intel processors are said to have "hyper-threading" (AMD has it too) and it is meant to be able to perform multiple "threads" or multitask much better.
据说英特尔处理器具有“超线程”(AMD 也有),它意味着能够更好地执行多个“线程”或多任务。
I am not sure about the logistics of how a thread is handled. I do recall hearing about the processor going back and forth between them, but I am not 100% sure about this and hopefully somebody else can answer that.
我不确定如何处理线程的后勤。我确实记得听说过处理器在它们之间来回切换,但我对此不是 100% 确定,希望其他人可以回答这个问题。
回答by Ion Todirel
I'm not really happy with any of those answers, so I'm going to add my own here :) A thread is a kernel abstraction for scheduling work on the processor, a thread is what the kernel gives you to manage the processor time and share work with others
我对这些答案中的任何一个都不满意,所以我将在这里添加我自己的 :) 线程是用于在处理器上调度工作的内核抽象,线程是内核为您管理处理器时间的工具并与他人分享工作