Chapter#03 - Threads & Multithreading Concept
Chapter#03 - Threads & Multithreading Concept
COURSE INSTRUCTOR:
ENGR. FARHEEN QAZI, ENGR. SYED HARIS MEHBOOB, MS. FALAK SALEEM
Overview
User Level Thread
Kernel Level Thread
Multithreading Models
Threading Issues
Objectives
Thread Usage
Responsiveness
Interactive application can delegate background functions to a
thread and keep running
Resource Sharing
Several different threads can access the same address space
Economy
Allocating memory and new processes is costly. Threads are much
‘cheaper’ to initiate.
Scalability
Use threads to take advantage of multiprocessor architecture
Multithreading Models
Simple Representation:
Each thread is represented simply by a PC, registers, stack and
a small control block, all stored in the user process address
space.
Simple Management:
Means that creating a thread, switching between threads and
synchronization between threads can all be done without
intervention of the kernel.
Any user-level thread performing a blocking system call will cause the
entire process to block, even if other threads are available to run.
Kernel Threads
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Kernel Threads Advantages
The kernel-level threads are slow and inefficient. For instance, threads
operations are hundreds of times slower than that of user-level
threads.
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Problems:
The global data may be inconsistent:
Was another thread in the process of updating it?
Data or critical code may be locked by another thread. That lock is copied
into child process, too.
Memory leaks
Thread (other) specific data not available
Recommendation: In multithreaded application, only use fork() after exec()
Thread cancellation
Example: When a user press the stop button on a web browser, the
thread loading a web page should be cancelled.
Asynchronous Cancellation
One thread immediately terminates the target thread.
Deferred Cancellation
The target thread can periodically check if it should terminate.
Useful when you do not have control over the thread creation
process (i.e., when using a thread pool)
Threads vs. Processes
Advantages of multithreading
Sharing between threads is easy
Faster creation
Disadvantages of multithreading
Ensure threads-safety
Bug in one thread can bleed to other threads, since they share the
same address space
Threads must compete for memory
Considerations
Dealing with signals in threads is tricky
All threads must run the same program
Sharing of files, users, etc
End of Chapter 3
Thank you