Overview
A thread — short for thread of execution — is a single sequence of programmed instructions that can be scheduled and executed independently of other sequences within the same program. Threads let a program divide work into multiple concurrently running units so that input/output, user interface rendering, background computation and event handling can proceed without blocking one another. For an introduction and general definitions see threads.
Characteristics and structure
Threads normally exist inside a process and share the process’s memory image, file descriptors, and other resources, while still maintaining their own execution context such as program counter, registers and stack. This close association makes threads relatively "lightweight" compared with separate processes (processes) because creating and switching between threads typically incurs less overhead than doing the same for full processes managed by the operating system. Threads are commonly identified by thread IDs and may be implemented at user level, kernel level, or via a hybrid model.
Lifecycle and scheduling
A thread’s lifecycle generally includes states such as ready, running, blocked (waiting), and terminated. The operating system or a user-level runtime schedules threads onto available CPU cores. When multiple threads run at once they achieve parallelism on multi-core hardware; when the system interleaves execution on a single core the effect is concurrency or pseudo‑parallelism. Scheduling policies, priorities and time slicing influence responsiveness and throughput.
Synchronization and hazards
Because threads share memory, programmers must coordinate access to shared data to avoid race conditions and inconsistent state. Common synchronization primitives include:
- Mutexes and locks — to enforce exclusive access to resources.
- Semaphores — counting mechanisms for limited resources.
- Condition variables and monitors — for waiting and signaling between threads.
- Read–write locks and atomic operations — for performance-sensitive cases.
Without proper synchronization threads may deadlock, livelock, or produce subtle bugs that are hard to reproduce.
Uses and examples
Threads are widely used in servers, graphical user interfaces, scientific computing and real-time systems. Common patterns include worker pools for processing tasks (tasks), background I/O threads to keep interfaces responsive, and parallel loops to exploit multiple CPU cores. Programming environments provide thread abstractions such as POSIX threads, language-level threads (for example Java threads), or lightweight "green" threads managed by a runtime.
History, distinctions and notable facts
The concept of threads evolved as operating systems and hardware supported multitasking and timesharing; designers introduced threads to reduce context-switch cost and enable finer-grained concurrency than full processes. Important distinctions include user-level versus kernel-level threads, and the trade-offs between ease of communication (shared memory) and isolation (separate processes). For more on processes and how they contrast with threads see process materials and operating system documentation at OS resources. Thread programming demands careful design, but when used appropriately it improves performance, responsiveness and resource utilization.