0% found this document useful (0 votes)
10 views15 pages

Thread

The document provides an overview of threading concepts, distinguishing between single-threaded and multi-threaded processes, and discusses the benefits of multi-threading such as efficiency and simplified code. It also covers multicore programming challenges, types of parallelism, user and kernel threads, and various threading models. Additionally, it addresses threading issues like signal handling and thread cancellation, along with methods for implicit threading and thread pools.

Uploaded by

ladybugmatto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Thread

The document provides an overview of threading concepts, distinguishing between single-threaded and multi-threaded processes, and discusses the benefits of multi-threading such as efficiency and simplified code. It also covers multicore programming challenges, types of parallelism, user and kernel threads, and various threading models. Additionally, it addresses threading issues like signal handling and thread cancellation, along with methods for implicit threading and thread pools.

Uploaded by

ladybugmatto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Thread

A thread is a small unit of a process that can run independently.


Single Thread
 Single Thread:
 A program that runs one task at a time.
 It can only do one thing, like fetching data or updating the display,
sequentially.
Multi-Thread:
 A program that can run multiple tasks at the same time using
separate threads.
 For example, it can update the display, fetch data, check spelling,
and handle network requests all at once.
Benefits of Multi-Threading
 Efficiency: Threads are lightweight and easier to create compared
to processes, making them faster.
 Simplified Code: Multi-threading can make coding easier and
more organized.
 Kernel Support: Most operating systems (kernels) support multi-
threading to manage tasks efficiently.
Single and Multithreaded Processes

Multithreaded Server Architecture

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

User Threads and Kernel Threads


User Threads
Definition: Managed by a user-level threads library, not by the
operating system.
Main Libraries:
 POSIX Pthreads: A standard for multithreading in C/C++.
 Windows Threads: Thread management in Windows applications.
 Java Threads: Built-in support for threads in Java applications.
Kernel Threads
Definition: Managed and supported directly by the operating system
(kernel).
Examples: Found in most general-purpose operating systems,
including:
 Windows
 Linux
 Mac OS X
 iOS
 Android
User and Kernel Threads

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:

 Each user-level thread matches to a kernel thread.


 Creating a user thread also creates a kernel thread.
 Offers more concurrency than many-to-one but can have
overhead, limiting the number of threads.
Examples:
 Windows
 Linux
Many-to-Many Model:
 Many user-level threads can be mapped to many kernel threads.
 The OS can create enough kernel threads for better performance.
Examples:
 Windows with the ThreadFiber package
 This model is not very common.
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to
kernel thread

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

Windows Multithreaded C Program


Windows Multithreaded C Program (Cont.)

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.

 Cancellation Disabled: If a thread has cancellation turned off, the


request waits until the thread enables it again.
 Default Behavior: The default cancellation type is deferred,
meaning cancellation only happens at specific points called
"cancellation points." For example, when the thread calls
pthread_testcancel().
 Cleanup: After cancellation, a cleanup handler runs to perform
any necessary cleanup.
 On Linux: Thread cancellation is managed using signals.

You might also like