Lecture_09 - Introduction to Threads
Lecture_09 - Introduction to Threads
Systems
COSC 22073/BECS 22233
Lesson 09: Introduction to Threads
A.M.K.S Aththanayake
Faculty of Science
University of Kelaniya
1
What is a Thread?
A thread is a path of execution within a process. A
process can contain multiple threads.
Improving the responsiveness of user interfaces.
Managing concurrent tasks. Threads within the same
process can communicate and share data more easily
than separate processes, which is a significant advantage
in many applications.
2
Why do we need Thread?
Most modern applications are multithreaded.
Threads run within the application.
Multiple tasks with the application can be implemented by separate threads:
Update display
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is light-weight.
Can simplify code, and increase efficiency.
Kernels are generally multithreaded
3
Single and Multithreaded Processes
4
Process vs Thread
5
Benefits of Threads
Concurrency: Threads allow multiple tasks to execute concurrently within the same program, which can lead to better
utilization of available CPU resources and faster task execution. This is particularly important in multi-core and multi-
processor systems.
Responsiveness: Threads can make applications more responsive. For example, in a graphical user interface, one thread can
handle user input and interface updates, while another thread can perform background tasks like file I/O or network
communication. This prevents the user interface from freezing during resource-intensive operations.
Efficient Resource Sharing: Threads within the same process share the same memory space and resources, such as file
handles and open network connections. This efficient resource sharing can lead to reduced memory overhead and improved
communication between threads.
Parallelism: Threads enable parallelism, allowing multiple threads to execute tasks simultaneously. This can significantly
improve the performance of applications that can benefit from dividing their work into smaller, parallel units, such as scientific
simulations, multimedia processing, and data processing..
Enhanced Scalability: In applications where the workload can vary, threads can be dynamically created and managed to adapt
to the current demand. This scalability can improve the overall efficiency of the application.
. 6
User Threads and Kernel Threads
7
User Threads and Kernel Threads
8
Multithreading Models
Many-to-one
One-to-One
Many-to-Many
Two-level Model
9
Multithreading Models
Many-to-One
10
Multithreading Models
One-to-One
11
Multithreading Models
Many-to-Many
12
Multithreading Models
Two-level Model
13
Multicore Programming
14
Concurrency vs. Parallelism
15
Multicore Programming
16
Data and Task Parallelism
17
Amdahl’s Law
18
Amdahl’s Law
Speedup = 1 / (S+{(1-S)/N})
= 1/ (0.25 + {(1 - 0.25)/2}
= 1/ (0.25 + 0.375) = 1/ 0.625 = 1.6
19
Thank You.
20