Nepedia
Back to Discover
Advanced Reader
💻

Thread (computing)

technology Maturity 7-9 Next article →

In computer science, a thread of execution is the smallest sequence of programmed instructions that a scheduler can manage independently. A scheduler is a part of the operating system that decides which tasks get to use the processor. Most threads exist as components within a larger unit called a process. While a process acts as a container for resources, a thread acts as the unit of execution itself.

Concepts- Program vs. Process vs. Thread
Concepts- Program vs. Process vs. Thread

This distinction is vital for modern computing performance. By using multiple threads, an application can perform many tasks at once. This allows for much more efficient use of a computer's hardware.

To understand how threads work, we must look at how they interact with a process. A process is a heavyweight unit that owns specific resources. These resources include memory for code and data, file handles, and sockets. When multiple threads exist within a single process, they share these resources. They specifically share the executable code and the values of dynamically allocated variables.

Multithreaded process
Multithreaded process

Because they share the same address space, threads can communicate very easily through shared data. However, this sharing creates a risk. If one thread performs an illegal operation, it can crash the entire process. This means one error can stop every other thread in that application.

There are several distinct types of threads used in different computing environments. Kernel threads are managed directly by the operating system. They are lightweight units of scheduling that exist within a process. At least one kernel thread must exist in every process. User threads are managed in userspace, meaning the kernel is unaware of them. Some implementations, like those used by virtual machines, call these "green threads." There are also fibers, which are even lighter units of scheduling. Fibers use cooperative scheduling, meaning a running fiber must explicitly yield control to allow another fiber to run.

The history of threading shows how computing has evolved over decades. Threads first appeared as "tasks" in IBM's OS/360 batch processing system in 1967. The term "thread" itself is credited to Victor A. Vyssotsky in 1966. In the summer of 1986, the Mach implementation of threads was described. Windows NT became the first version of Windows to support threads when it was released in 1993. In 1995, the IEEE defined the pthreads API. This provided a standard interface for portable multithreaded programming across Unix-like systems. This standardization allowed developers to write code that worked on many different types of computers.

Threading is significant because of how it handles hardware resources. Creating and destroying processes is relatively expensive for a computer. This is because the system must acquire or release many resources. In contrast, thread switching is relatively cheap. It requires a context switch, which involves saving and restoring registers and the stack pointer. However, it does not change virtual memory, making it "cache-friendly." On single-core systems, the CPU uses time slicing to switch between threads. The CPU switches so frequently, often every 100 to 200 milliseconds, that users perceive the tasks as running in parallel.

Modern hardware has changed how we use threads through multi-core technology. Until the early 2000s, most desktop computers used only a single-core CPU. In 2002, Intel introduced simultaneous multithreading in the Pentium 4 processor, known as hyper-threading. By 2005, dual-core processors became more common, such as the Pentium D from Intel and the Athlon 64 X2 from AMD. On these multi-core systems, multiple threads can execute in true parallel. Each core can execute a separate thread at the exact same time. This allows software to achieve much higher performance levels by utilizing every part of the chip.

Finally, threading connects to broader concepts of scheduling and concurrency. Scheduling can happen at the kernel level or the user level. It can also be preemptive or cooperative. In preemptive scheduling, the operating system can interrupt a thread at any time to let another run. This gives the system fine-grained control over execution time. In cooperative multithreading, threads must voluntarily relinquish control. This can be efficient, but it can also cause problems if a thread refuses to yield. Understanding these different models helps programmers choose the best way to build fast and stable software.

701 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (2)
File:Multithreaded process.svg
Multithreaded process.svg
File:Concepts- Program vs. Process vs. Thread.jpg
Concepts- Program vs. Process vs. Thread.jpg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.