By richard


2011-03-05 05:16:48 8 Comments

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.

12 comments

@Balaji Radhakrishnan 2018-03-10 07:28:47

Let me explain the difference between process and threads first.

A process can have {1..N} number of threads. A small explanation on virtual memory and virtual processor.

Virtual memory

Used as a swap space so that a process thinks that it is sitting on the primary memory for execution.

Virtual processor

The same concept as virtual memory except this is for processor. To a process, it will look it's the only thing that is using the processor.

OS will take care of allocating the virtual memory and virtual processor to a process and performing the swap between processes and doing execution.

All the threads within a process will share the same virtual memory. But, each thread will have their individual virtual processor assigned to them so that they can be executed individually.

Thus saving the memory as well as utilizing the CPU to its potential.

@Ion Todirel 2018-02-03 09:14:22

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

@AleksandrH 2018-09-22 12:45:35

-1 Threads need not be created by the kernel. Threads with kernel-level support are indeed scheduled by the kernel (where some sort of syscall is issued). But there are also threads with user library-level support, with the thread table residing in user space.

@Leos313 2016-07-01 12:14:52

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

Here, you can find more information about the topic. That was also my information-source.

@chatuur 2016-04-14 08:10:02

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

@Suhail Mumtaz Awan 2017-06-30 08:16:22

that's the real deal answer

@Dinesh Kumar 2018-08-01 17:26:14

Helped a lot. This is what explanation looks like.

@desa 2018-09-05 01:27:00

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!

@Rodolfo Beguiristain 2016-12-10 19:23:03

If you want an answer in english, heres what my teacher told me when I asked him:

"You know how when you run a java program, it runs the program from top bottom? Well a thread is basically a way for you to get around that. Like opening two text boxes at once, or having a timer running at the same time as a bunch of other stuff."

I swear that's almost exactly what he said.

@MattSt 2017-01-22 19:52:07

A process usually consists of at least one thread known as the primary thread. stackoverflow.com/questions/7623493/zero-threaded-process Threads are not used only in parallel programming hence you cannot define threads solely by their use for parallelism. Different processes can also be used for parallel programming. Even if they consist of threads, the communication between threads and between processes is different.

@Valentin Radu 2016-04-13 20:09:01

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.

@pwnall 2011-03-05 05:29:13

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

@Solomon Slow 2014-06-25 19:47:31

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.

@aerijman 2019-04-20 16:41:00

@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)?

@Joey Adams 2011-03-05 05:26:25

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.

@committedandroider 2015-03-06 06:24:24

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?

@Joey Adams 2015-03-06 18:49:22

@committedandroider: Good call. I edited my answer to say that processes don't share memory (by default), but threads share all memory.

@Ben Voigt 2011-03-05 05:23:06

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

@Greg Hewgill 2011-03-05 05:24:26

+1. A thread isn't anything more "concrete" than a set of register values.

@richard 2011-03-05 05:25:22

What "set of values"? What are they? How do they define a thread?

@Ben Voigt 2011-03-05 05:31:10

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

@richard 2011-03-05 05:33:11

Thanks Ben. That's very helpful.

@committedandroider 2015-03-06 06:18:43

@BenVoigt For processors with multiple cores, is a thread not an "independent set of values for the processor registers"?

@Ben Voigt 2015-03-06 15:12:12

@committedandroider: No, a thread has state for only one core. To have multiple cores active, you need multiple threads.

@BKSpurgeon 2016-04-13 12:30:17

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

@Levi Fuller 2019-04-09 02:53:30

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 stack

@hobbs 2011-03-05 05:27:53

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.

@committedandroider 2015-03-06 06:25:01

Is a context switch just when the processor goes from one thread to another(whether in the same process or another)?

@Flipper 2011-03-05 05:25:14

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.

@committedandroider 2015-03-06 06:21:36

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?

@Jeremy Friesner 2015-03-06 19:31:45

It's an optimization that gives better performance for some use cases. You can read about hyper threading here: en.wikipedia.org/wiki/Hyper-threading

@Orbit 2011-03-05 05:21:59

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.

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

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

@richard 2011-03-05 05:22:59

What is it that makes it "something tangible"? Is it just that data stored in the TLS and its call stack?

@Orbit 2011-03-05 05:25:35

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.

@richard 2011-03-05 05:29:08

Enlighten me . . . so what is the answer?

@Orbit 2011-03-05 05:31:05

@Richard not looking to get into a debate about semantics, just phrased my answer to attempt to clarify conceptually to the OP.

@committedandroider 2015-03-06 06:19:46

@richard what's the TLS?

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How can I use threading in Python?

34 Answered Questions

[SOLVED] What is the difference between a process and a thread?

42 Answered Questions

[SOLVED] "implements Runnable" vs "extends Thread" in Java

23 Answered Questions

[SOLVED] What is a daemon thread in Java?

17 Answered Questions

[SOLVED] How can one use multi threading in PHP applications

  • 2008-09-16 09:56:31
  • Steve Obbayi
  • 440641 View
  • 394 Score
  • 17 Answer
  • Tags:   php multithreading

32 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1637093 View
  • 3925 Score
  • 32 Answer
  • Tags:   http rest definition

21 Answered Questions

13 Answered Questions

[SOLVED] What resources are shared between threads?

7 Answered Questions

[SOLVED] Do zombies exist ... in .NET?

Sponsored Content