What is a "thread" (really)?
Asked Answered
T

12

369

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

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.

Toritorie answered 5/3, 2011 at 5:16 Comment(8)
"Process" is no less of an abstract term.Heida
Is thread local storage just the call stack for the thread?Stableboy
The answers below are... abstract. In simpler terms (and glossing over some details): once upon a time, a computer program could only do one thing at once. So it did A, then after that B, then C, then... . In modern systems, this isn't ideal; for example you want to keep browsing the web while downloading a file. So programs now have one or more 'threads'. Each 'thread' can only do one thing at once, but different threads can do things simultaneously. Thread 1 can do A, then B, then C; thread 2 can do X, then Y, then Z. B can't start until A has finished, but A and X can happen at once.Seasonal
@Seasonal that is great but how is that different from a process?Felid
@Felid the basic difference between a thread and process (and really the most important difference) is that two or more threads can share the same spaces in memory, i.e use the same resources, whereas two processes must exist in different memory spaces. Does that make sense?Cinthiacintron
@IkechukwuAnude as threads shares resources of process (which creates these threads) and each threads has it's own stack, does that mean the stack allocated to process is divided among all the different threads? If it's true, does that mean if we increase number of threads, size of stack per thread will decrease?Lucillalucille
@MegaLegend found an answer yet?Dan
@Dan As far as I have understood. Thread is really just an abstract idea. Say you have three functions that you want to execute concurrently. Then you will create three threads and load one function in each of the thread. You can consider thread as a way for OS to store information about how much of the loaded function (on that thread)has been executed. Since each function needs its own call stack, thus one is allocated to each thread (new stack is allocated to each thread and parent process stack is not divided among them). Some resources like code block are shared among each threads.Lucillalucille
S
214

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

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

Soliz answered 5/3, 2011 at 5:23 Comment(21)
+1. A thread isn't anything more "concrete" than a set of register values.Aswan
What "set of values"? What are they? How do they define a thread?Toritorie
@Richard: The exact list of CPU registers depends on the architecture, but instruction pointer and stack pointer are pretty much universal. They define a thread insofar as when this thread (set of register values) is loaded in the processor core, the thread is running. The processor is fetching instructions demanded by the thread and updating the thread registers. When a context switch is needed, the processor saves this set of register values into memory and loads a set belonging to a different thread, typically as part of the interrupt servicing logic.Soliz
@BenVoigt For processors with multiple cores, is a thread not an "independent set of values for the processor registers"?Stableboy
@committedandroider: No, a thread has state for only one core. To have multiple cores active, you need multiple threads.Soliz
Hi thx @BenVoigt. A few clarifications that noobs like me may stumble over: what is meant by "processor registers"? What is meant by "instruction pointer" and "stack pointer"?Zootechnics
Google-fu: Instruction Pointer -> is a register that holds the memory address of the instruction to be executed next. The CPU is hard-wired to read the instruction pointer and execute the instruction at that particular address. Processor Register -> The registers are the places where the values that the CPU is actually working on are located (e.g. variable values). Stack Pointer -> Is a small register that stores the address of the last program request in a stackEisenach
@LeviFuller During parallel execution of threads and processes, which instruction does the instruction pointer to?Jeffery
@ajaysinghnegi: A single processor core doesn't do parallel execution, it does timeslicing. On a multicore system (parallel processing), you can't use the definite article "the" to talk about registers like the instruction pointer, because there are many copies, one in each CPU core. (Note that all multisocket systems are also multicore even if you don't have "multicore processor" units installed in the sockets, because the one core per socket still results in multiple cores in the system).Soliz
With HyperThreading, "core" is often used to mean a pair of closely-coupled cores which share compute resources, but each of these two still has its own set of registers and control logic... for the purposes of this answer "processor core" means each hardware threading unit, regardless of whether it has shared or dedicated compute associated.Soliz
A "set of values for the processor registers" is not a thread. That's a thread context. It's part of a thread, but no more so than all the variables that are exclusively used by the thread (including its entire stack) or, the kernel variables that describe the thread. A computer scientist would say that a "thread of execution" is a particular sequence of operations—one that is "threaded" through your program's code. I tell noobs that a thread is like an agent that carries out your instructions and, that you can have more than one such agent working for you at the same time.Cellophane
@SolomonSlow: A thread context is what makes a thread a thread. All the other associated things are supplementary, not definitional.Soliz
@BenVoigt, Yup, just like how the wheels are what make a car a car. A car isn't any use if it's got no wheels. All that other stuff—frame, suspension, drive train, steering—that's all just bells and whistles, but you ain't going anywhere if you've got no wheels.Cellophane
@SolomonSlow: The kernel object associated with the thread, that enforces access control, allows you to find the thread by its ID, etc is more like power steering. You can have a car without power steering and it's still a car. But man doesn't that hydraulic assist make life convenient. Likewise the kernel structure contains thread-management data (aka metadata). It's closely coupled to the thread. But metadata isn't the thing, it's data about the thing.Soliz
@BenVoigt Finally, which one is correct? Thread is a set ov values for the CPU registers or Thread is a sequence of instructions? Which one comes first? And which one should be deduced from the other? I'm confused :(Utilitarianism
@SolomonSlow Finally, which one is correct? Thread is a set ov values for the CPU registers or Thread is a sequence of instructions? Which one comes first? And which one should be deduced from the other? I'm confused :(Utilitarianism
@MohammadMehdiSarfejoo Why should only one answer be "correct?" Why should one be "derived from the other?" A computer scientist might have a different way of thinking about what "thread" means from the way a kernel developer thinks about it.Cellophane
@SolomonSlow so You mean they both have same meaning and both of them are in the same direction although they are different ways of thinking about "thread". Did I understand this correctly?Utilitarianism
@MohammadMehdiSarfejoo, At a higher level, they both are talking about the same thing, but they emphasize different aspects of it. A computer scientist is more interested in reasoning about sequences of instructions, in how they are interleaved, and in how they can interact with each other. A kernel developer is more interested in how the system keeps track of which sequence of instructions it is executing at any given moment and, in how it switches from one to another.Cellophane
@MohammadMehdiSarfejoo: "which one should be deduced from the other?" is exactly what the last paragraph of my answer explainsSoliz
@BenVoigt is it like the context is what makes a thread that thread?Wina
G
339

A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.

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.

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.

On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.

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

Gettysburg answered 5/3, 2011 at 5:29 Comment(2)
A better analogy would equate person with CPU (both do something), and equate book with address-space (both just exist). That way, bookmarks in different books are like threads in different processes. A single book with more than one bookmark would be the analog of a multi-threaded process, which is what people usually mean when they say "threads." It works for a single processor machine, but it breaks down somewhat when you talk about multi-processing. Nobody cares which CPU executes function f(), but it does matter which person reads chapter 11.Cellophane
@pwnall, thanks a lot for digesting difficult concepts for others like me! Is multithreading involved in multiprocessing ( or running a process in parallel on many CPUs, in case I am using the wrong term)?Broider
S
214

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

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

Soliz answered 5/3, 2011 at 5:23 Comment(21)
+1. A thread isn't anything more "concrete" than a set of register values.Aswan
What "set of values"? What are they? How do they define a thread?Toritorie
@Richard: The exact list of CPU registers depends on the architecture, but instruction pointer and stack pointer are pretty much universal. They define a thread insofar as when this thread (set of register values) is loaded in the processor core, the thread is running. The processor is fetching instructions demanded by the thread and updating the thread registers. When a context switch is needed, the processor saves this set of register values into memory and loads a set belonging to a different thread, typically as part of the interrupt servicing logic.Soliz
@BenVoigt For processors with multiple cores, is a thread not an "independent set of values for the processor registers"?Stableboy
@committedandroider: No, a thread has state for only one core. To have multiple cores active, you need multiple threads.Soliz
Hi thx @BenVoigt. A few clarifications that noobs like me may stumble over: what is meant by "processor registers"? What is meant by "instruction pointer" and "stack pointer"?Zootechnics
Google-fu: Instruction Pointer -> is a register that holds the memory address of the instruction to be executed next. The CPU is hard-wired to read the instruction pointer and execute the instruction at that particular address. Processor Register -> The registers are the places where the values that the CPU is actually working on are located (e.g. variable values). Stack Pointer -> Is a small register that stores the address of the last program request in a stackEisenach
@LeviFuller During parallel execution of threads and processes, which instruction does the instruction pointer to?Jeffery
@ajaysinghnegi: A single processor core doesn't do parallel execution, it does timeslicing. On a multicore system (parallel processing), you can't use the definite article "the" to talk about registers like the instruction pointer, because there are many copies, one in each CPU core. (Note that all multisocket systems are also multicore even if you don't have "multicore processor" units installed in the sockets, because the one core per socket still results in multiple cores in the system).Soliz
With HyperThreading, "core" is often used to mean a pair of closely-coupled cores which share compute resources, but each of these two still has its own set of registers and control logic... for the purposes of this answer "processor core" means each hardware threading unit, regardless of whether it has shared or dedicated compute associated.Soliz
A "set of values for the processor registers" is not a thread. That's a thread context. It's part of a thread, but no more so than all the variables that are exclusively used by the thread (including its entire stack) or, the kernel variables that describe the thread. A computer scientist would say that a "thread of execution" is a particular sequence of operations—one that is "threaded" through your program's code. I tell noobs that a thread is like an agent that carries out your instructions and, that you can have more than one such agent working for you at the same time.Cellophane
@SolomonSlow: A thread context is what makes a thread a thread. All the other associated things are supplementary, not definitional.Soliz
@BenVoigt, Yup, just like how the wheels are what make a car a car. A car isn't any use if it's got no wheels. All that other stuff—frame, suspension, drive train, steering—that's all just bells and whistles, but you ain't going anywhere if you've got no wheels.Cellophane
@SolomonSlow: The kernel object associated with the thread, that enforces access control, allows you to find the thread by its ID, etc is more like power steering. You can have a car without power steering and it's still a car. But man doesn't that hydraulic assist make life convenient. Likewise the kernel structure contains thread-management data (aka metadata). It's closely coupled to the thread. But metadata isn't the thing, it's data about the thing.Soliz
@BenVoigt Finally, which one is correct? Thread is a set ov values for the CPU registers or Thread is a sequence of instructions? Which one comes first? And which one should be deduced from the other? I'm confused :(Utilitarianism
@SolomonSlow Finally, which one is correct? Thread is a set ov values for the CPU registers or Thread is a sequence of instructions? Which one comes first? And which one should be deduced from the other? I'm confused :(Utilitarianism
@MohammadMehdiSarfejoo Why should only one answer be "correct?" Why should one be "derived from the other?" A computer scientist might have a different way of thinking about what "thread" means from the way a kernel developer thinks about it.Cellophane
@SolomonSlow so You mean they both have same meaning and both of them are in the same direction although they are different ways of thinking about "thread". Did I understand this correctly?Utilitarianism
@MohammadMehdiSarfejoo, At a higher level, they both are talking about the same thing, but they emphasize different aspects of it. A computer scientist is more interested in reasoning about sequences of instructions, in how they are interleaved, and in how they can interact with each other. A kernel developer is more interested in how the system keeps track of which sequence of instructions it is executing at any given moment and, in how it switches from one to another.Cellophane
@MohammadMehdiSarfejoo: "which one should be deduced from the other?" is exactly what the last paragraph of my answer explainsSoliz
@BenVoigt is it like the context is what makes a thread that thread?Wina
H
85

In order to define a thread formally, we must first understand the boundaries of where a thread operates.

A computer program becomes a process when 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 thread is a sequence of such instructions within a program that can be executed independently of other code. The figure shows the concept: enter image description here

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

Here, graphically, the concept is represented.

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 System by Edward Lee and 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).

Harbourage answered 1/7, 2016 at 12:14 Comment(1)
Thanks, this makes perfect sense.Epigrammatize
M
53

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.
Mozzarella answered 5/3, 2011 at 5:26 Comment(2)
You say that "processes share nothing (by default)" but in your analogy, you state that "processes are like two people using two different computers, who use the network to share data when necessary" So they do share something?Stableboy
@committedandroider: Good call. I edited my answer to say that processes don't share memory (by default), but threads share all memory.Mozzarella
I
52

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.

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:

• The browser process 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.

Renderer processes 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-in process 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.

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.

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.

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)

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.

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:

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

Indented answered 14/4, 2016 at 8:10 Comment(3)
that's the real deal answerMucin
Helped a lot. This is what explanation looks like.Further
A big value of this answer that it provides a reference book where you can find more details if it is needed. Thank you @chatuur!Shagbark
A
8

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.

The kernel introduces the thread and process concept to manage the memory and instructions order in a meaningful way.

Aleut answered 13/4, 2016 at 20:9 Comment(0)
C
8

A thread is a set of (CPU)instructions which can be executed.

But in order to have a better understanding of what a thread is, some computer architecture knowledge is required.

What a computer does, is to follow instructions and manipulate data. RAM is the place where the instructions and data are saved, the processor uses those instructions to perform operations on the saved data.

The CPU has some internal memory cells called, registers. It can perform simple mathematical operations with numbers stored in these registers. It can also move data between the RAM and these registers. These are examples of typical operations a CPU can be instructed to execute:

  • Copy data from memory position #220 into register #3
  • Add the number from register #3 to the number from register #1.

The collection of all operations a CPU can do is called instruction set. Each operation in the instruction set is assigned a number. Computer code is essentially a sequence of numbers representing CPU operations, these numbers are stored in RAM.

The CPU works in a never-ending loop, always fetching and executing an instruction from memory. At the core of this cycle is the PC register, or Program Counter. It's a special register that stores the memory address of the next instruction to be executed.

The CPU will:

  1. Fetch the instruction at the memory address given by the PC,
  2. Increment the PC by 1,
  3. Execute the instruction,
  4. Go back to step 1.

The CPU can be instructed to write a new value to the PC, causing the execution to branch, or "jump" to somewhere else in the memory. And this branching can be conditional. For instance, a CPU instruction could say: "set PC to address #200 if register #1 equals zero". This allows computers to execute stuff like this:

if  x = 0
    compute_this()
else
    compute_that()

Resources used from Computer Science Distilled.

Cerebellum answered 4/2, 2021 at 13:31 Comment(0)
V
7

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 its own process, they are running separately inside the process. On multi-core machines, 2 threads could run at the same time.

http://en.wikipedia.org/wiki/Simultaneous_multithreading

http://www.intel.com/intelpress/samples/mcp_samplech01.pdf

Vinitavinn answered 5/3, 2011 at 5:21 Comment(5)
What is it that makes it "something tangible"? Is it just that data stored in the TLS and its call stack?Toritorie
That it isn't just an abstraction for understanding... If it really were just a single thread that ran back and forth masquerading as multiple threads, the OP would be right, but yes, I would say that this data would make it tangible.Vinitavinn
Enlighten me . . . so what is the answer?Toritorie
@Richard not looking to get into a debate about semantics, just phrased my answer to attempt to clarify conceptually to the OP.Vinitavinn
@Toritorie what's the TLS?Stableboy
S
7

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.

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.

Stricklan answered 5/3, 2011 at 5:25 Comment(3)
How do Intel processors handle multiple threads better? With a single core, only one thread should execute at a time. I agree with the processor going back and forth. You can't really do that better can you?Stableboy
It's an optimization that gives better performance for some use cases. You can read about hyper threading here: en.wikipedia.org/wiki/Hyper-threadingSpermogonium
A thread is not like a task. Tasks are units of work that need to be done. Threads are like agents who perform tasks. The distinction is important because a common trope in multi-threaded programs is that when some task needs to be performed, it does not matter which thread performs it. That trope is best embodied by a thread pool, which is an object that manages a collection of worker threads. When a task object is submitted to a thread pool, any one of the pool's worker threads may be chosen to perform the task.Cellophane
H
6

The answer varies hugely across different systems and different implementations, but the most important parts are:

  1. 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).
  2. A thread has a lifetime (it can be created by another thread, and another thread can wait for it to finish).
  3. 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, everything is threads -- a process is merely a type of 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.

Heida answered 5/3, 2011 at 5:27 Comment(1)
Is a context switch just when the processor goes from one thread to another(whether in the same process or another)?Stableboy
E
4

Just as a process represents a virtual computer, the thread abstraction represents a virtual processor.

So threads are an abstraction.

Abstractions reduce complexity. Thus, the first question is what problem threads solve. The second question is how they can be implemented.

As to the first question: Threads make implementing multitasking easier. The main idea behind this is that multitasking is unnecessary if every task can be assigned to a unique worker. Actually, for the time being, it's fine to generalize the definition even further and say that the thread abstraction represents a virtual worker.

Now, imagine you have a robot that you want to give multiple tasks. Unfortunately, it can only execute a single, step by step task description. Well, if you want to make it multitask, you can try creating one big task description by interleaving the separate tasks you already have. This is a good start but the issue is that the robot sits at a desk and puts items on it while working. In order to get things right, you cannot just interleave instructions but also have to save and restore the items on the table.

This works, but now it's hard to disentangle the separate tasks by simply looking at the big task description that you created. Also, the ceremony of saving and restoring the items on the tabe is tedious and further clutters the task description.

Here is where the thread abstraction comes in and saves the day. It lets you assume that you have an infinite number of robots, each sitting in a different room at its own desk. Now, you can just throw task descriptions in a pot and everything else is taken care of by the thread abstraction's implementer. Remember? If there are enough workers, nobody has to multitask.

Often it is useful to indicate your perspective and say robot to mean real robots and virtual robot to mean the robots the thread abstraction provides you with.

At this point the problem of multitasking is solved for the case when the tasks are fully independent. However, wouldn't it be nice to let the robots go out of their rooms, interact and work together towards a common goal? Well, as you probably guessed, this requires coordination. Traffic lights, queues - you name it.

As an intermediate summary, the thread abstraction solves the problem of multitasking and creates an opportunity for cooperation. Without it, we only had a single robot, so cooperation was unthinkable. However, it has also brought the problem of coordination (synchronization) on us. Now we know what problem the tread abstraction solves and, as a bonus, we also know what new challenge it creates.


But wait, why do we care about multitasking in the first place?

First, multitasking can increase performance if the tasks involve waiting. For example, while the washing machine is running, you can easily start preparing dinner. And while your dinner is in the over, you can hang out the clothes. Note that here you wait because an independent component does the job for you. Tasks that involve waiting are called I/O bound tasks.

Second, if multitasking is done rapidly, and you look at it from a bird's eyes view, it appears as parallelism. It's a bit like how the human eye perceives a series of still images as motion if shown in quick succession. If I write a letter to Alice for one second and to Bob for one second as well, can you tell if I wrote the two letters simultaneously or alternately, if you only look at what I'm doing every two seconds? Search for Multitasking Operating System for more on this.


Now, let's focus on the question of how the thread abstraction can be implemented.

Essentially, implementing the thread abstraction is about writing a task, a main task, that takes care of scheduling all the other tasks.

A fundamental question to ask is: If the scheduler schedules all tasks and the scheduler is also a task, then who schedules the scheduler?

Let's brake this down. Say you write a scheduler, compile it and load it into the main memory of a computer at the address 1024, which happens to be the address that is loaded into the processor's instruction pointer when the computer is started. Now, your scheduler goes ahead and finds some tasks sitting precompiled in the main memory. For example, a task starts at the address 1,048,576. The scheduler wants to execute this task so it loads the task's address (1,048,576) into the instruction pointer. Huh, that was quite an ill considered move because now the scheduler has no way to regain control from the task it has just started.

One solution is to insert jump instructions to the scheduler (address 1024) into the task descriptions before execution. Actually, you shouldn't forget to save the items on the desk the robot is working at, so you also have to save the processor's registers before jumping. The issue here is that it is hard to tell where to insert the jump instructions. If there are too many, they create too much overhead and if there are too few of them, one task might monopolize the processor.

A second approach is to ask the task authors to designate a few places where control can be transferred back to the scheduler. Note that the authors don't have to write the logic for saving the registers and inserting the jump instruction because it suffices that they mark the appropriate places and the scheduler takes care of the rest. This looks like a good idea because task authors probably know that, for example, their task will wait for a while after loading and starting a washing machine, so they let the scheduler take control there.

The problem that neither of the above approaches solve is that of an erroneous or malicious task that, for example, gets caught up in an infinite loop and never jumps to the address where the scheduler lives.

Now, what to do if you cannot solve something in software? Solve it in hardware! What is needed is a programmable circuitry wired up to the processor that acts like an alarm clock. The scheduler sets a timer and its address (1024) and when the timer runs out, the alarm saves the registers and sets the instruction pointer to the address where the scheduler lives. This approach is called preemptive scheduling.


Probably by now you start to sense that implementing the thread abstraction is not like implementing a linked list. The most well-known implementers of the thread abstraction are operating systems. The threads they provide are sometimes called kernel-level threads. Since an operating system cannot afford losing control, all major, general-purpose operating systems uses preemptive scheduling.

Arguably, operating systems feel like the right place to implement the thread abstraction because they control all the hardware components and can suspend and resume threads very wisely. If a thread requests the contents of a file stored on a hard drive from the operating system, it immediately knows that this operation will most likely take a while and can let another task occupy the processor in the meanwhile. Then, it can pause the current task and resume the one that made the request, once the file's contents are available.

However, the story doesn't end here because threads can also be implemented in user space. These implementers are normally compilers. Interestingly, as far as I know, kernel-level threads are as powerful as threads can get. So why do we bother with user-level threads? The reason, of course, is performance. User-level threads are more lightweight so you can create more of them and normally the overhead of pausing and resuming them is small.

User-level threads can be implemented using async/await. Do you remember that one option to achieve that control gets back to the scheduler is to make task authors designate places where the transition can happen? Well, the async and await keywords serve exactly this purpose.


Now, if you've made it this far, be prepared because here comes the real fun!

Have you noticed that we barely talked about parallelism? I mean, don't we use threads to run related computations in parallel and thereby increase throughput? Well, not quiet.. Actually, if you only want parallelism, you don't need this machinery at all. You just create as many tasks as the number of processing units you have and none of the tasks has to be paused or resumed ever. You don't even need a scheduler because you don't multitask.

In a sense, parallelism is an implementation detail. If you think about it, implementers of the thread abstraction can utilize as many processors as they wish under the hood. You can just compile some well-written multithreaded code from 1950, run it on a multicore today and see that it utilizes all cores. Importantly, the programmer who wrote that code probably didn't anticipate that piece of code being run on a multicore.

You could even argue that threads are abused when they are used to achieve parallelism: Even though people know they don't need the core feature, multitasking, they use threads to get access to parallelism.


As a final thought, note that user-level threads alone cannot provide parallelism. Remember the quote from the beginning? Operating systems run programs inside a virtual computer (process) that is normally equipped with a single virtual processor (thread) by default. No matter what magic you do in user space, if your virtual computer has only a single virtual processor, you cannot run code in parallel.

So what do we want? Of course, we want parallelism. But we also want lightweight threads. Therefore, many implementers of the thread abstraction started to use a hybrid approach: They start as many kernel-level threads as there are processing units in the hardware and run many user-level threads on top of a few kernel-level threads. Essentially, parallelism is taken care of by the kernel-level and multitasking by the user-level threads.


Now, an interesting design decision is what threading interface a language exposes. Go, for example, provides a single interface that allows users to create hybrid threads, so called goroutines. There is no way to ask for, say, just a single kernel-level thread in Go. Other languages have separate interfaces for different kinds of threads. In Rust, kernel-level threads live in the standard library, while user-level and hybrid threads can be found in external libraries like async-std and tokio. In Python, the asyncio package provides user-level threads while multithreading and multiprocessing provide kernel-level threads. Interestingly, the threads multithreading provides cannot run in parallel. On the other hand, the threads multiprocessing provides can run in parallel but, as the library's name suggests, each kernel-level thread lives in a different process (virtual machine). This makes multiprocessing unsuitable for certain tasks because transferring data between different virtual machines is often slow.

Further resources:

Operating Systems: Principles and Practice by Thomas and Anderson

Concurrency is not parallelism by Rob Pike

Parallelism and concurrency need different tools

Asynchronous Programming in Rust

Inside Rust's Async Transform

Rust's Journey to Async/Await

What Color is Your Function?

Why goroutines instead of threads?

Why doesn't my program run faster with more CPUs?

John Reese - Thinking Outside the GIL with AsyncIO and Multiprocessing - PyCon 2018

Evered answered 16/6, 2022 at 1:29 Comment(0)
R
1

A thread is no more no less abstract than a process is.

A process is a set of resources necessary to execute a code, thus very roughly, memory (data and code) and threads of execution.

A thread is a set of resources to execute a code in the context of a process, so it has, roughly its own stack and instruction pointer, but shares everything else with other threads in the same process, thus all the memory allocated to the process.

Rooted answered 30/1 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.