Lec2-A Multithreading
Lec2-A Multithreading
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.
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.
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
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
Client 1
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
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.