"Thread" - what is it, definition of the term
A thread is a lightweight execution unit inside a process, consisting of a sequential series of instructions that the scheduler can run concurrently with other units; it shares the process’s address space and resources while maintaining an independent stack, program counter, and register set.
Detailed information
A lightweight execution stream represents a single flow of control inside a program. It possesses its own stack, program counter, and register set while sharing the address space, global variables, and open files with other streams in the same process.
Creation occurs through system interfaces that allocate a stack, initialize registers, and schedule the new stream for execution. Termination frees the stack and any thread‑local storage, returning control to the operating system.
Scheduling mechanisms allocate CPU time slices to each stream. Preemptive policies interrupt streams based on timer events, while cooperative schemes rely on explicit yield calls. The scheduler decides which stream runs on which core, enabling parallelism on multicore hardware.
Synchronization ensures safe access to shared resources. Common primitives include:
- Mutexes: exclusive locks that protect critical sections.
- Semaphores: counters that regulate access to a bounded number of resources.
- Condition variables: signals that awaken waiting streams when a condition becomes true.
- Atomic operations: lock‑free modifications of single memory locations.
Performance considerations focus on minimizing context‑switch latency, reducing contention for locks, and balancing work across cores. Because each stream shares most memory with its peers, the memory footprint remains low compared to full processes, allowing large numbers to coexist.
An analogy can be drawn with small rodents such as rats and mice. Each animal navigates a complex environment, interacts with shared resources, and can be observed in large groups without overwhelming the ecosystem. Similarly, numerous lightweight execution streams operate concurrently, cooperating and competing for limited resources while maintaining a minimal individual impact on the system.