SEET4623
NETWORK PROGRAMMING
MULTI-THREADING
DR. NURZAL EFFIYANA BINTI GHAZALI
Terminology
• Multiprocessing
• A system that has two or more than two processors
• Multithreading
• A CPU generate multiples threads out of a task and process all of them
simultaneously (extended from multitasking)
• Multiprogramming
• More than one program run in the main memory
• Multitasking
• When a single resource is used to process multiple tasks (context switching)
Multiprocessing
CPU CPU CPU
Cache Cache Cache
Register Register Register
Memory
Multithreading
Thread 1 User 1
Thread 2 User 2
Thread 3 User 3
Thread ⋮ User ⋮
Thread n n
Database
Server
Multiprogramming
Main
memory
Processor Idle Idle
Time
Multitasking
CPU
Thread Control Block (TCB)
• Thread Identifier: Unique id (TID) is assigned to every new thread
• Stack pointer: Points to thread’s stack in the process. Stack contains the local
variables under thread’s scope.
• Program counter: A register which stores the address of the instruction
currently being executed by thread.
• Thread state: Can be running, ready, waiting, start or done.
• Thread register set: Registers assigned to thread for computations.
• Parenting process Pointer: A pointer to the Process control block (PCB) of the
process that the thread lives on.
Thread
Control
Block
(TCB)
Multi-threading
• Module
thread
threading
• Method provides by the Thread Class
run(): entry point for the thread
start(): starts a thread by calling run method
join([time]): wait for thread to terminate
isAlive(): check whether a thread is still executing
getName(): returns the name of a thread
setName(): sets a name of a thread
Using function
Taking
function
Using function
Taking
arguments
Using function
Process
Main
thread
t1 t2
Target: task 1 Target: task 2
Variable: name Variable: name
Thread Synchronization
• Thread synchronization is the concurrent execution of two or more threads that
share critical resources. Threads should be synchronized to avoid critical resource
use conflicts. Otherwise, conflicts may arise when parallel-running threads attempt
to modify a common variable at the same time.
Variable Z Critical section
Thread 1 Thread 2 Thread 3
Race Condition
• Concurrent accesses to shared resource can lead to race condition.
• A race condition occurs when two or more threads can access shared data and they
try to change it at the same time.
• As a result, the values of variables may be unpredictable and vary depending on the
timings of context switches of the processes.
Lock
• One synchronization technique.
• A lock is an abstraction that allows at most one thread to own it at a
time. Holding a lock is how one thread tells other threads: “I’m changing this
thing, don’t touch it right now.”
• Locks have two operations:
acquire allows a thread to take ownership of a lock. If a thread tries to acquire a
lock currently owned by another thread, it blocks until the other thread releases
the lock. At that point, it will contend with any other threads that are trying to
acquire the lock. At most one thread can own the lock at a time.
release relinquishes ownership of the lock, allowing another thread to take
ownership of it.
Lock
acquire([blocking]) : To acquire a lock. A lock can be blocking or nonblocking.
When invoked with the blocking argument set to True (the default), thread
execution is blocked until the lock is unlocked, then lock is set to locked and
return True.
When invoked with the blocking argument set to False, thread execution is
not blocked. If lock is unlocked, then set it to locked and return True else
return False immediately.
release() : To release a lock.
When the lock is locked, reset it to unlocked, and return. If any other threads
are blocked waiting for the lock to become unlocked, allow exactly one of
them to proceed.
If lock is already unlocked, a ThreadError is raised.
Advantages & Disadvantages of Multithreading
Advantages:
• It doesn’t block the user. This is because threads are independent of each other.
• Better use of system resources is possible since threads execute tasks parallely.
• Enhanced performance on multi-processor machines.
• Multi-threaded servers and interactive GUIs use multithreading exclusively.
Disadvantages:
• As number of threads increase, complexity increases.
• Synchronization of shared resources (objects, data) is necessary.
• It is difficult to debug, result is sometimes unpredictable.
• Potential deadlocks which leads to starvation, i.e. some threads may not be served
with a bad design
• Constructing and synchronizing threads is CPU/memory intensive.