Module 4
Threads and Concurrency
1. What is a Thread?
A thread is the smallest unit of execution inside a process. A process (a running program) can have multiple threads that run tasks simultaneously.
Example:
A web browser process may have multiple threads:
one thread loads a webpage
one thread plays video
one thread handles user input
Components of a Thread
A thread usually contains:
Thread ID
Program counter
Register set
Stack
Unlike processes, threads share memory and resources with other threads in the same process.
2. Difference Between Process and Thread
Process | Thread |
|---|---|
Independent program in execution | Small unit inside a process |
Has its own memory space | Shares memory with other threads |
Heavyweight | Lightweight |
Slower to create | Faster to create |
Threads are cheaper and faster than processes.
3. Why Multithreading is Used
Most modern applications use multithreading because it improves performance and responsiveness.
Benefits of Multithreading
1. Responsiveness
A program can continue running even if part of it is busy.
Example:
While downloading a file, the interface still works.
2. Resource Sharing
Threads share the same memory and resources within a process.
This allows faster communication compared to processes.
3. Economy
Threads require fewer resources than processes.
Creating threads is faster than creating processes.
4. Scalability
Programs can use multiple CPU cores, allowing better performance on modern multicore systems.
4. Multicore Programming
Modern computers often have multiple CPU cores. Multicore programming allows programs to divide work among cores.
Challenges include:
dividing tasks
balancing workload
managing data sharing
debugging
5. Concurrency vs Parallelism
These two terms are often confused.
Concurrency
Multiple tasks make progress at the same time, but not necessarily running simultaneously.
Example:
One CPU switches between tasks quickly.
Parallelism
Multiple tasks run at the exact same time using multiple CPU cores.
Example:
Two threads run simultaneously on two cores.
6. Types of Parallelism
Data Parallelism
The same operation is performed on different pieces of data.
Example:
Processing different parts of a large image.
Task Parallelism
Different threads perform different tasks.
Example:
One thread calculates data while another handles user input.
7. Amdahl’s Law
Amdahl’s Law describes how much performance improvement we get by adding more processors.
Key idea:
Not all parts of a program can run in parallel. Some parts must run sequentially.
If a program has a large sequential portion, adding more cores will not greatly improve performance.
8. Types of Threads
User Threads
Managed by user-level libraries, not the operating system.
Examples:
POSIX Pthreads
Java threads
Windows threads
Advantages:
fast creation
efficient
Disadvantages:
if one thread blocks, all threads may block.
Kernel Threads
Managed by the operating system kernel.
Examples:
Linux
Windows
macOS
Kernel threads allow better multitasking and scheduling.
9. Multithreading Models
These models describe the relationship between user threads and kernel threads.
Many-to-One Model
Many user threads map to one kernel thread.
Problem:
If one thread blocks, the whole process stops.
Also cannot run threads in parallel.
One-to-One Model
Each user thread maps to one kernel thread.
Advantages:
better concurrency
true parallelism on multicore systems
Disadvantage:
creating too many threads may slow down the system.
Used by:
Windows
Linux
Many-to-Many Model
Many user threads map to many kernel threads.
Advantages:
flexibility
efficient resource use
10. Thread Libraries
Thread libraries provide functions to create and manage threads.
Common libraries include:
Pthreads
Standard for UNIX systems.
Used in:
Linux
macOS
Windows Threads
API used for multithreading in Windows applications.
Java Threads
Managed by the Java Virtual Machine (JVM).
Threads can be created by:
extending the
Threadclassimplementing the
Runnableinterface.
11. Implicit Threading
In implicit threading, the system automatically manages threads.
The programmer focuses on tasks instead of thread management.
Examples include:
Thread pools
Fork-Join
OpenMP
Grand Central Dispatch
12. Thread Pool
A thread pool is a collection of pre-created threads waiting for tasks.
Advantages:
reduces overhead of creating threads
controls number of threads
improves performance.
13. Fork-Join Parallelism
Fork-Join is a model used to divide tasks.
Steps:
Fork – split a large task into smaller tasks.
Execute tasks in parallel.
Join – combine the results.
This is commonly used in parallel programming.
14. Threading Issues
Some challenges occur when working with threads.
fork() and exec()
When a process creates another process:
should it copy all threads?
or only the calling thread?
Different operating systems handle this differently.
Signal Handling
Signals notify processes when events occur.
Example events:
errors
interrupts
termination requests.
Thread Cancellation
Stopping a thread before it finishes.
Two types:
Asynchronous cancellation
thread stops immediately.
Deferred cancellation
thread checks periodically if it should stop.
Thread-Local Storage (TLS)
Each thread has its own copy of data.
This prevents conflicts between threads.