Newby Coder header banner

Thread

What is a Thread in programming

A thread is a sequence of instructions within a program that can be executed independently

The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process

Analogy

Consider a student is given multiple (written) assignments

The student decided to do all assignments in parallel so that he doesn't get bored or something

He can recognize the progress of an assignment by looking at the recent page that was written

Some assignments can be linked or inter-dependent

The student is like the CPU and each assignment is like a thread

A new assignment is like a new thread

That is similar to how a single processor generally implements multi-threading by time slicing or switching between multiple threads

A multi-processor scenario is similar to multiple students working on the assignments, such that they can recognize each other's work


Process

A computer program becomes a process when it is loaded 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, etc


Multi-threading

Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources

The threads of a process share its executable code and the values of its dynamically allocated variables and non-thread-local global variables

Language such as Javascript is single-threaded