njosefbeck / devops-and-backend-dev-roadmap

To keep track of my personal progress through the backend and devops roadmaps found at roadmap.sh
0 stars 0 forks source link

Threads & Concurrency #4

Open njosefbeck opened 5 years ago

njosefbeck commented 5 years ago

What are threads?

Previously we learned that a program is just a set of instructions for the computer, and a process is the execution of those instructions.

"A thread is the basic unit to which the operating system allocates processor time." "A thread is a path of execution within a process. A process can contain multiple threads." "A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc." "The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces. Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space." "A thread is the unit of execution within a process. A process can have anywhere from just one thread to many threads."

"When a process starts, it is assigned memory and resources. Each thread in the process shares that memory and resources. In single-threaded processes, the process contains one thread. The process and the thread are one and the same, and there is only one thing happening.

In multithreaded processes, the process contains more than one thread, and the process is accomplishing a number of things at the same time (technically, sometimes it’s almost at the same time—read more on that in the “What about Parallelism and Concurrency?” section below)."

"In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.[1] The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its dynamically allocated variables and non-thread-local global variables at any given time."

Concurrency vs. Parallelism

"A question you might ask is whether processes or threads can run at the same time. The answer is: it depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time. In this case, the CPU is shared among running processes or threads using a process scheduling algorithm that divides the CPU’s time and yields the illusion of parallel execution. The time given to each task is called a “time slice.” The switching back and forth between tasks happens so fast it is usually not perceptible. The terms parallelism (genuine simultaneous execution) and concurrency (interleaving of processes in time to give the appearance of simultaneous execution), distinguish between the two types of real or approximate simultaneous operation."

Resources

https://www.backblaze.com/blog/whats-the-diff-programs-processes-and-threads/ https://en.wikipedia.org/wiki/Thread_(computing)