0% found this document useful (0 votes)
25 views

Lec2-A Multithreading

The document discusses serial vs parallel computing and processes vs threads. Serial computing involves breaking a problem into discrete instructions that are executed one after another by a single CPU. Parallel computing involves breaking a problem into parts that can be solved concurrently using multiple CPUs. A process is a program in execution and defines its own memory space, while a thread is the unique flow of control within a process and multiple threads can execute concurrently within the same process.

Uploaded by

Jawad Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lec2-A Multithreading

The document discusses serial vs parallel computing and processes vs threads. Serial computing involves breaking a problem into discrete instructions that are executed one after another by a single CPU. Parallel computing involves breaking a problem into parts that can be solved concurrently using multiple CPUs. A process is a program in execution and defines its own memory space, while a thread is the unique flow of control within a process and multiple threads can execute concurrently within the same process.

Uploaded by

Jawad Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

MULTITHREADING

Process & Thread


WEEK 09
Serial vs. Parallel
Computing

Page 3
Serial Computing
Traditionally, software has been written for serial
computation:
◦To be run on a single computer having a single Central Processing
Unit (CPU);
◦A problem is broken into a series of discrete instructions.
◦Instructions are executed one after another.
◦Only one instruction may execute at any moment in time.

Page 5
Serial Computing

Page 4
Parallel Computing
The simultaneous use of multiple compute resources
to solve a computational problem.

To be run using multiple CPUs


◦A problem is broken into discrete parts that can be solved
concurrently
◦Each part is further broken down to a series of
instructions

Instructions from each part execute simultaneously on


different CPUs

Page 7
Parallel Computing
Example

Page 8
Processes and
Threads

0
Process and Thread
A process is a computer program in execution.
A thread is the execution path of a program.
A thread defines a unique flow of control.
Multiple threads can execute concurrently within the
same process.

Difference between Process and Thread


Process
• Processes are the basic activities of a system
• Processes use resources exclusively, e.g.,
• Memory:
• each process has an own virtual address space

2
• other processes cannot read or write into this memory
• processes thus “think” that they are “alone” in
the memory
• Processor:
• scheduling is transparent for processes
• processes think they have their own processor on
which only they are executed
Single vs. Multithreaded Program
Single-Machine Parallelism
• First and second generation systems: only
single program in execution
• For better resource utilization: multiple
programs loaded, “switching” of active
process
1
• Preemptive multitasking: “switching”
performed by the operating system,
e.g., via timer interrupt
Context Switch
• OS performs context switches storing
registers and process counter in Process
Control Block (PCB)
• Selecting next process (PCB)
• Restoring registers, instruction pointer,
virtual memory table pointer
• Flushing of caches
Context Switch
• OS performs context switches storing
registers and process counter in Process
Control Block (PCB)
• Restoring registers, instruction pointer,
virtual memory table pointer
• Flushing of caches
time
• Processes run quasi-parallel: OS performs
P1 P2 P3
context switches
progress
Process: Pros and Cons
• Advantages?
• Security: other processes cannot read memory /
confidential data
• Safety: if one process fails, it cannot influence
other processes directly
• Disadvantages?
• Inter-process communication (IPC) slow
• context switch slow
• explicit sharing of data/information complicated
Modern Applications &
Systems
• Operating System Level
• Multitasking: multiple applications running at
once
• Application Level
• Multithreading: multiple operations performed
at the same time
A single threaded
program
class ABC
begin
{
….
body
public void main(..)
{

..
}
end
}
A Multithreaded
Program
Main Thread

start start start

Thread A Thread B Thread C

Threads may switch or exchange data/results


A Multithreaded
Program
§ What is the motivation behind multithreading?
• The most relevant reason for multithreading is to separate
multiple tasks: one or more which is time critical
and might be subject to interference by theexecution of
other tasks
• For example: our stock market related software is able to
download data from the web ( Yahoo Finance for example).
• It takes 2-3 mins to fetch the data BUT we want to
make sure the application is not frozen !
A Multithreaded Program
§ By default programming languages are sequential :
they execute the statements and commands one by
one (line by line)

public static void main(String[]args) {

initializeArrays ();
downloadStocks( );
11
initalizeTimeSeriesM
odels();
makePredictions();
}
§ In a single threaded program these methods will
be called one by one: we have to wait for them to finish one
by one ...
§ Not the best solution possible: time consuming
operations may freeze
Solution: we the application
create and the
a distinct thread users
for the may not
download
know what's operation and during this procedure the user can do whatever
happening!
he/she wants in the application
Multithreaded
Applications
§ Modern Systems
• Applications perform many tasks at once!
• This means that… there are multiple
threads within a single process.
Background printing

GUI rendering

Application core
logic

Word count
Multithreaded
Applications
§ Multithreaded Web
Server
Process Request Client 1
Web/FTP
server

Process Request Client 2

Client 1

Process Request Client N

Client 2

Client N
Single and Multithreaded
Processes
threads are light-weight processes within a
process
Single-threaded Process Multi-threaded Process
Threads of
Execution

Single instruction stream Common Multiple instruction stream


Address Space
Single and Multithreaded Processes
code data files code data files

registers stack registers registers registers

stack stack stack

thread thread

single-threaded multithreaded
process process
When to use Threads?
If an application involves complicated and time consuming
operations, then set different execution paths or threads, with
each thread performing a particular operation.
Thread Taxonomy
Thread Level Parallelism
Threads in C#
System.Threading Namespace
Provides classes and interfaces that enable multithreaded programming.
Thread Class
It allows creating and accessing individual threads in
a multithreaded application.
The first thread to be executed in a process is called the
main
thread.
When a C# program starts execution, the main thread is
automatically created.
Thread Life
Cycle
The life cycle of a thread starts when an object of the
Thread class is created
It ends when the thread is terminated or completes
execution.
Thread Life Cycle States
Thread Life Cycle
Following are the various states in the life cycle of a thread:
◦The Unstarted State: It is the situation when the instance of
the thread is created but the Start() method is not called.
◦The Runnable/Ready/Started State: It is the situation
when the thread is ready to run and waiting CPU cycle.
◦The Running State: At the time of execution, thread is
in running state.
◦The Not Runnable State: A thread is not executable, when:
Sleep()/Wait() method has been called or Blocked by I/O
operations
◦The Dead/ Abort State: the thread completes execution or
is terminated.
Thread Class Methods
Method Action
Start Causes a thread to start to run.
Sleep Pauses a thread for a specified time.
Abort Stops a thread when it reaches a safe point.
Resume Restarts a suspended thread
Join Causes the current thread to wait for another
thread to finish.
Safe Points
 Safe points are locations in code where it is safe for the
common language runtime to perform automatic
garbage collection, the process of releasing unused
variables and reclaiming memory.

 When the Abort() or Suspend() method of a thread is


called, the common language runtime analyzes the code
and determines the location of an appropriate location
for the thread to stop running.
Thread Class Properties
CurrentThread Gets the currently running
thread.
IsAlive Gets a value indicating the execution status of
the current thread.
Gets or sets a value indicating whether or not a
IsBackground thread is a background thread.
Gets or sets the name of the thread. Gets
Name or sets a value indicating the scheduling
Priority priority of a thread.
Gets a value containing the states of the
current thread.
ThreadState
The Main Thread
When a C# program starts execution, the main thread is
automatically created.
The threads created using the Thread class are called the
child threads of the main thread.
We can access a thread using the CurrentThread property
of the Thread class.
Example: Using the Main Thread
using System.Threading;
s t a t i c void Main(string[] args)
{
Thread th = Thread.CurrentThread;
th.Name = "MainThread";
Console.WriteLine("This i s { 0 } " , th.Name);
Console.ReadKey();
}
Example: Creating a Child Thread
public s t a t i c void CallToChildThread()
{
Console.WriteLine("Child thread s t a r t s " ) ;
}

s t a t i c void Main(string[] args)


{
Console.WriteLine ("In Main: Creating the Child thread" );
Thread childThread = new Thread(CallToChildThread);
childThread.Start();
Console.ReadKey();
}
ThreadState Enum
The ThreadState enumeration defines a set of all possible
execution states for threads.

It's of interest only in a few debugging scenarios.

Your code should never use the thread state to synchronize


the activities of threads.
Aborte 256 The thread state includes AbortRequested and the thread is
d now dead, but its state has not yet changed to Stopped.
AbortRequested 128 The Abort(Object) method has been invoked on the thread,
but the thread has not yet received the
pending ThreadAbortException that will attempt to terminate it.
The thread is being executed as a background thread, as opposed
Backgroun 4 to a foreground thread. This state is controlled by setting
d the IsBackground property.
The thread has been started and not yet stopped. The
Running 0 thread has stopped.
Stopped 16 thread is being requested to stop. This is for internal use only.
The
StopRequeste 1 thread has been suspended.
The
d The thread is being requested to suspend.
Suspended 64 Start() method has not been invoked on the thread.
The
SuspendRequested The
2 thread is blocked. This could be the result of calling
Unstarted 8 Sleep(Int32) or Join(), of requesting a lock - or of
WaitSleepJoi 32 waiting on a thread synchronization object such
n as ManualResetEvent.
Managing Thread using Sleep()
Method
public s t a t i c void CallToChildThread()
{
Console.WriteLine("Child thread s t a r t s " ) ;
/ / the thread i s paused f o r 5000 milliseconds
i n t sleepfor = 5000;
Console.WriteLine(" Child Thread Paused fo r {0}
seconds",
sleepf
or/
1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
Managing Thread using Sleep() Method
s t a t i c void Main(string[] args)
{
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(CallToChildThread);
childThread.Start();
Console.ReadKey();
}
Destroying Threads using Abort()
Method
s t a t i c void Main(string[] args)
{
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(CallToChildThread);
childThread.Start();
/ / sto p the main thread fo r some time
Thread.Sleep(2000);
//now abort the child
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort();
Console.ReadKey();
}
Destroying Threads using Abort()
Method
public s t a t i c void CallToChildThread()
{ try
{ Console.WriteLine("Child thread s t a r t s " ) ;
/ / do some work, l i k e counting to 10
fo r ( i n t counter = 0; counter <= 10; counter++)
{
Thread.Sleep(500);
Console.WriteLine(counter);
}
Console.WriteLine("Child
Thread Completed");
}
catch (ThreadAbortException e)
{
Console.WriteLine("Thread Abort
Exception"); }
}
Program Output

You might also like