Thread
Thread
Multicore Programming
Multicore and Multiprocessor Systems
Definition: These systems have multiple processors or cores that can
perform tasks at the same time.
Challenges for Programmers
1. Dividing Activities:
Tasks must be split into smaller parts that can run simultaneously.
2. Balancing:
Ensuring each processor or core has an equal amount of work to avoid
some being overloaded while others are idle.
3. Data Splitting:
Dividing data so that different threads can work on it without conflicts.
4. Data Dependency:
Managing how threads rely on each other’s data, which can cause
delays.
5. Testing and Debugging:
Finding and fixing issues in a system with multiple tasks running at once
can be more complex.
Key Concepts
1. Parallelism:
The ability to perform multiple tasks at the same time.
2. Concurrency:
The ability to make progress on more than one task, even if they aren't
running at the exact same moment.
3. Single Processor/Core:
Uses a scheduler to manage and switch between tasks, providing a
sense of concurrency.
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Parallelism on a multi-core system:
Multicore Programming
Types of Parallelism
Data Parallelism:
Distributes parts of the same data across multiple cores.
Each core performs the same operation on its subset of data.
Task Parallelism:
Distributes different threads across cores.
Each thread performs a unique operation or task.
Data and Task Parallelism
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Many-to-One Model:
Multiple user-level threads are mapped to a single kernel thread.
If one thread blocks, all threads block.
Only one thread can be in the kernel at a time, limiting parallel
execution on multicore systems.
Examples:
1. Solaris Green Threads
2. GNU Portable Threads
One-to-One Model:
Thread Libraries
Definition: A thread library is a set of programming tools that provide
an API (Application Programming Interface) to help programmers
create and manage threads easily. Threads are essential for running
multiple tasks at the same time within a program.
Implementation Methods
1. User-Space Library:
This type of library operates entirely in user space, meaning it
doesn’t rely on the operating system for thread management.
It allows for faster thread operations since there’s no need to
switch to kernel mode, but it can face limitations, especially in
blocking situations.
2. Kernel-Level Library:
This library is supported by the operating system (kernel),
allowing the OS to manage threads directly.
It enables better scheduling and resource management, allowing
multiple threads to run in parallel on multicore systems.
Pthreads (POSIX Threads)
Definition: Pthreads is a popular thread library that adheres to
the POSIX standard. It can be implemented as either a user-level
or kernel-level library.
Standard: The POSIX standard (IEEE 1003.1c) defines a consistent
API for thread creation, synchronization, and management across
different operating systems.
Specification: While the API specifies how the threads should
behave (like creating, joining, and synchronizing), the actual
implementation can vary by the developers of the library. This
allows flexibility in how it is used.
Usage: Pthreads are widely used in UNIX-like operating systems,
such as Linux and Mac OS X, making it a common choice for
developers working in these environments.
Pthreads Example
Pthreads Code for Joining 10 Threads
Implicit Threading
Definition: Implicit threading is becoming more popular as programs
use more threads. It helps make programs easier to write and manage.
How It Works: Instead of programmers managing threads directly,
compilers and run-time libraries handle thread creation and
management automatically.
Methods of Implicit Threading
Thread Pools:
A collection of pre-created threads that wait for tasks to perform.
Advantages:
Faster Task Handling: Using an existing thread is usually quicker
than creating a new one.
Controlled Thread Count: The number of active threads can be
limited to the size of the pool.
Task Management: Separates the task from the thread
management, allowing for different scheduling strategies (e.g.,
running tasks at specific times).
Support: The Windows API includes support for thread pools, making it
easier for developers to use this method.
Threading Issues
Semantics of fork() and exec():
1. fork(): It can duplicate either just the calling thread or all threads,
depending on the system. Some UNIX versions have different
implementations.
2. exec(): Replaces the running process, including all threads, and
works as usual.
Signal Handling:
Definition: Signals are notifications to a process about events (like
interruptions).
Signal Handler: A function that processes signals. There are two types:
Default Handler: Handled by the kernel.
User-Defined Handler: Custom function defined by the
programmer.
Delivery Options in Multi-Threading:
Send the signal to the specific thread it applies to.
Send the signal to all threads.
Send to selected threads.
Assign one thread to receive all signals for the process.
Thread Cancellation:
Definition: Stopping a thread before it completes its task.
Target Thread: The thread that is being canceled.
Approaches:
Asynchronous Cancellation: Stops the target thread immediately.
Deferred Cancellation: Allows the target thread to check and
decide if it should be canceled at certain points.
Thread Cancellation (Cont.)
Requesting Cancellation: When a cancellation request is made, it
does not immediately stop the thread. The actual cancellation
depends on the thread's state.