THREADS AND
MULTITHREADING
Mr. Vincent L. Gonzaga
OVERVIEW
• A thread is the smallest unit of execution within a
process. Threads allow multiple tasks to be executed
concurrently within the same process, improving efficiency
and performance. Modern operating systems use
multithreading to handle multiple operations
simultaneously, making better use of multi-core
processors.
KEY CONCEPTS IN THIS
CHAPTER:
[Link] Threads: A thread is a lightweight process
that shares resources (memory, files) with other threads within
the same process.
[Link] Libraries & APIs: Programming interfaces (such as
Pthreads, Windows Threads, and Java Threads) provide ways
to create and manage threads.
[Link] Threading: The OS or language runtime
automatically manages threads without explicit user control.
[Link] in Multithreading: Synchronization, data
consistency, and resource allocation issues need to be
addressed.
[Link] Support for Multithreading: Operating systems like
Windows and Linux offer different methods for thread creation
and management.
MULTICORE PROGRAMMING
A multicore processor is a CPU with multiple processing units
(cores) on a single chip. Each core can execute its own thread
independently, leading to better performance.
Why Multicore Programming?
• Improved Performance: Multiple threads can run simultaneously
on different cores.
• Better Resource Utilization: Uses CPU cores efficiently, reducing
idle time.
• Parallel Execution: Allows for tasks like video rendering and
gaming to run faster.
Challenges in Multicore Programming
[Link] Management: Proper synchronization is needed to
prevent race conditions.
[Link] Balancing: Ensuring all CPU cores are utilized equally.
[Link] Sharing: Threads must manage shared memory safely to
avoid inconsistencies.
MULTITHREADING MODELS
A multithreading model determines how user threads map to kernel threads.
1. Many-to-One Model
• Multiple user threads are mapped to a single kernel thread.
• Disadvantage: If one user thread blocks, all threads stop.
• Example: Early implementations of Java threads.
2. One-to-One Model
• Each user thread is mapped to a separate kernel thread.
• Advantage: If one thread blocks, others can continue.
• Disadvantage: More kernel threads consume more system resources.
• Example: Windows and Linux use this model.
3. Many-to-Many Model
• Many user threads are mapped to many kernel threads, improving
flexibility.
• Advantage: Threads can be scheduled efficiently across available CPU
cores.
• Example: Solaris uses this model.
THREAD LIBRARIES
A thread library provides functions to create and manage
threads in a program. The three major thread libraries are:
1. Pthreads (POSIX Threads)
• Used in Linux and UNIX systems.
• Provides functions like pthread_create(), pthread_join(),
and pthread_mutext_lock()
2. Windows Threads
• Windows OS provides its own API for managing threads.
• Functions include CreateThread(), WaitForSingleObject(),
and TerminateThread()
3. Java Threads
• Java provide built-in thread support using the Thread
class and Runnable interface.
• Methods include start(), run(), and sleep().
IMPLICIT THREADING
Implicit threading allows the OS or a library to handle thread
management automatically, reducing the complexity for programmers.
Strategies for Implicit Threading
[Link] Pools:
Pre-created threads handle tasks instead of creating new ones each time.
Example: Web servers use thread pools for handling multiple client
requests.
[Link]-Join Model:
Tasks are divided into smaller parts (forked) and processed in parallel,
then combined (joined).
Example: Parallel processing in scientific computations.
[Link] (Open Multi-Processing):
A standard for parallel programming that automatically assigns threads to
tasks.
Example: Used in applications requiring high-performance computing.
THREADING ISSUES
Multithreaded programming introduces several challenges:
1. Race Conditions
• Occurs when multiple threads access shared
resources simultaneously, leading to unpredictable
results.
• Solution: Use synchronization mechanisms like mutexes
and semaphores.
2. Deadlocks
• Happens when two or more threads wait for
resources that are held by each other, leading to a
cycle of waiting.
• Solution: Implement a resource hierarchy or use timeout
mechanisms.
3. Starvation
• When a low-priority thread never gets CPU time
because high-priority threads always execute first.
• Solution: Use priority aging to gradually increase priority
over time.
4. Thread Synchronization
• Ensures threads access shared data in a controlled
manner.
• Tools for synchronization:
• Mutex (Mutual Exclusion): Allows only one thread to
access a resource at a time.
• Semaphore: Controls access by allowing multiple threads
a limited number of entries.
• Condition Variables: Used to signal when a condition is
met (e.g., a resource is available).
OS SUPPORT FOR THREADS
Different OS implementations handle threads in unique ways.
1. Windows Threads
• Uses a one-to-one threading model.
• Provides functions for thread creation, synchronization, and
termination.
• Uses objects like Critical Sections, Mutexes, and
Semaphores for synchronization.
2. Linux Threads
• Uses the pthread library for multithreading.
• Threads are created using pthread() and managed using
pthread_join().
• Uses spinlocks and futexes (fast user-space mutexes) for
thread synchronization.
SUMMARY
THANK YOU!