1. What is a Thread?
A thread is a basic unit of CPU utilization, consisting of a program counter, stack,
registers, and thread ID.
Traditional processes have a single thread, but multi-threaded processes have multiple
threads sharing common resources such as code and data.
2. Motivation for Threads
Threads allow efficient execution of tasks that can be performed independently within a
process.
Threads are useful for handling multiple tasks concurrently, improving responsiveness
and minimizing blocking. Examples include background tasks in word processors and
web servers handling multiple requests simultaneously.
3. Benefits of Multi-threading
Responsiveness: Threads allow for faster responses even if other threads are blocked.
Resource sharing: Threads share common resources, enabling multiple tasks in a single
address space.
Economy: Threads are more efficient than processes as they share resources, reducing
overhead.
Scalability: Multi-threading allows tasks to run on multiple processors, maximizing CPU
utilization.
4. Multicore Programming
With multi-core chips, threads can be spread across cores, enabling parallel processing.
New scheduling algorithms are required to optimize the use of multiple cores.
5. Programming Challenges
Multi-core chips introduce challenges like identifying tasks that can run concurrently,
balancing workload, data splitting, managing dependencies, and debugging parallel
programs.
6. Types of Parallelism
Data parallelism: Dividing data among threads and performing the same task on each
subset (e.g., image processing).
Task parallelism: Dividing different tasks among threads to be performed
simultaneously.
7. Thread Models
User Threads: Managed by a user-level thread library (no kernel support).
Kernel Threads: Managed by the OS kernel.
Thread models for managing user and kernel threads include:
o Many-to-One: Many user threads mapped to one kernel thread.
o One-to-One: One kernel thread per user thread.
o Many-to-Many: Multiple user threads mapped to an equal or smaller number of
kernel threads.
8. Thread Libraries
Thread libraries provide APIs to create and manage threads. Major libraries include:
o POSIX Pthreads: Can be implemented in user or kernel space.
o Win32 threads: Kernel-level on Windows.
o Java threads: Thread management depends on the OS and JVM.
9. Implicit Threading
Compiler and run-time libraries can handle threading challenges automatically, using
tools like Thread Pools, OpenMP, and Grand Central Dispatch (GCD) to simplify multi-
threading in C, C++, and Java.
10. Threading Issues
Signal Handling: Determines how signals are delivered to threads (e.g., to specific
threads or all threads).
Thread Cancellation: Threads can be canceled either asynchronously or deferred, with
potential challenges in resource management.
Thread-Local Storage: Allows threads to have their own local data that is not shared
with other threads.
Scheduler Activations: Virtual processors (Lightweight Processes) map user threads to
kernel threads and manage scheduling dynamically.
11. Operating-System Examples
Windows XP: Uses a one-to-one thread model and supports fibers for many-to-many.
Linux: Uses clone() for creating threads, with the flexibility to share resources between
parent and child tasks based on flags.
Summary:
Threads are essential for efficient modern computing, allowing processes to perform multiple
tasks concurrently, improving responsiveness, and optimizing resource usage. However, they
introduce complexities like debugging and managing dependencies in parallel tasks. Multi-core
processors and threading models help scale these tasks across processors, but careful
management and the right thread library are essential for effective multi-threaded
programming.