Multithreading
Introduction to Multithreading
1 © 2012 WIPRO LTD | WWW.WIPRO.COM
Agenda
Introduction
2 © 2012 WIPRO LTD | WWW.WIPRO.COM
Objectives
At the end of this module, you will be able to:
–Understand what is a Thread
3 © 2012 WIPRO LTD | WWW.WIPRO.COM
Introduction to Multithreading
4 © 2012 WIPRO LTD | WWW.WIPRO.COM
Need for Multithreading
Have you faced the following
situations:
Your browser cannot skip to the
next web page because it is
downloading a file?
You cannot enter text into your
current document until your word
processor completes the task of
saving the document to disk
5 © 2012 WIPRO LTD | WWW.WIPRO.COM
What is Multitasking?
• Multitasking is synonymous with process-based
multitasking, whereas multithreading is synonymous with
thread-based multitasking
• All modern operating systems support multitasking
• A process is an executing instance of a program
• Process-based multitasking is the feature by which the
operating system runs two or more programs concurrently
6 © 2012 WIPRO LTD | WWW.WIPRO.COM
Example
• You might have come across people doing multiple things
at the same time: A person talking on the phone while
having lunch or watching TV
• In a computer, you can run multiple programs at the same
time. Like you can play a song using Media Player while
typing a Word document
7 © 2012 WIPRO LTD | WWW.WIPRO.COM
What is Multithreading?
• In multithreading, the thread is the smallest unit of code
that can be dispatched by the thread scheduler
• A single program can perform two tasks using two
threads
• Only one thread will be executing at any given point of
time given a single-processor architecture
8 © 2012 WIPRO LTD | WWW.WIPRO.COM
Examples
• Computer games are best examples of multithreading
• You might have seen that in most of the ‘race’ games,
other cars or bikes will be competing with your car/bike.
These are nothing but threads.
9 © 2012 WIPRO LTD | WWW.WIPRO.COM
Examples
• Imagine that you need to paint your house. You can
employ one painter who will take 10 days to complete the
work or you can employ 10 painters who will finish the
work in one day.
• In this case the 10 painters will be painting at the same
time. That is 10 threads will be executing at the same
time.
One thread Multiple threads
10 © 2012 WIPRO LTD | WWW.WIPRO.COM
Examples
• When you start typing in a MS Word document, you can
see that the incorrectly spelled words are underlined with
a red wavy line. Like:
• This is done by spell check feature of MS Word. The spell
check is running in parallel while we type in a Word
document. This is handled by a separate thread.
11 © 2012 WIPRO LTD | WWW.WIPRO.COM
Multitasking Vs. Multithreading
Compared to multithreading, multitasking is characterized
by the following:
• Each process requires its own separate address space
• Context switching from one process to another is a CPU-
intensive task needing more time
• Inter-process communication between processes is again
expensive as the communication mechanism has to span
separate address spaces
These are the reasons why processes are referred to as
heavyweight tasks
12 © 2012 WIPRO LTD | WWW.WIPRO.COM
Multitasking Vs. Multithreading (Contd.).
• Threads cost less in terms of processor overhead because
of the following reasons:
– Multiple threads in a program share the same address
space and they are part of the same process
– Switching from one thread to another is less CPU-
intensive
– Inter-thread communication, on the other hand, is less
expensive as threads in a program communicate within
the same address space
• Threads are therefore called lightweight processes
13 © 2012 WIPRO LTD | WWW.WIPRO.COM
Uses of Multithreading
• A multithreaded application performs two or more
activities concurrently
• It is accomplished by having each activity performed by a
separate thread
• Threads are the lightest tasks within a program, and they
share memory space and resources with each other
14 © 2012 WIPRO LTD | WWW.WIPRO.COM
Single-Threaded Systems
• Single-threaded systems use an approach called an
event loop with polling
• In this model:
– A single thread of control runs in an infinite loop
– Polling a single event queue to decide which
instruction to execute next
– Until this instruction returns, nothing else can happen
in the system
– This results in wastage of precious CPU cycles
15 © 2012 WIPRO LTD | WWW.WIPRO.COM
Summary
• In this module, you were able to:
– What is a multithreading
– What is Multitasking?
– Uses of Multithreading
16 © 2012 WIPRO LTD | WWW.WIPRO.COM
Thank You
17 © 2012 WIPRO LTD | WWW.WIPRO.COM