Threads
Threads
1
Threads
Thread represents a basic unit of CPU utilization, and threads belonging to
the same process share many of the process resources, including code and
data
If a process has multiple threads, it can perform multiple tasks
simultaneously
Most software applications running on modern computers are multithreaded
Types of Parallelism
Data Parallelism: Focuses on distributing pieces of the same dataset
across different cores for simultaneous execution of the same type of
operation
o For example, if two cores are used to sum an array of N elements,
elements [0]..[(N/2)-1] are summed on core 1, and elements
[N/2]..[N-1] are summed on core 2
Multithreading Models
Thread support can be provided either at the user level (user threads)
or at the kernel level (kernel threads)
User threads are managed by user applications, while kernel threads
are managed by the operating system
Operating systems such as Windows, Linux, Unix, Mac OS X, and
Solaris support kernel threads
▪ Pthreads
▪ POSIX Pthreads is an API defined by the IEEE 1003.1c standard for creating
and managing threads
▪ Pthreads is used in Linux, Unix, Mac OS X, and Solaris operating systems
▪ Windows does not support the Pthreads standard
▪ In Pthreads, each thread is created as a separate function, and all threads share
global data
Threads
▪ Pthreads
▪ Example of Pthreads Program
▪ A program using Pthreads may include the following:
o A header file for thread functions
o Defining an ID for the new thread
o Setting the attributes for the new thread (e.g., stack size)
o Specifying the starting point for the thread (a function)
o Creating the thread with a fork-join strategy
▪ Multiple threads can be created and managed using this method
Threads
▪ Windows Threads
▪ Windows' thread library is similar to Pthreads in many aspects. Each thread is
created as a separate function, and all threads share global data.
Windows threads are kernel-level threads
▪ Example of Windows Threads: In Windows, thread creation might involve the
following steps:
o Use a header file for thread functions
o Define the starting point for the thread
o Use a system call like WaitForMultipleObjects() to allow multiple threads to run
synchronously
Threads
▪ Java Threads
▪ Java threads are designed to work on any system that supports the Java Virtual
Machine (JVM). The Java thread API is available for Windows, Linux, Unix, Mac
OS X, and Android
▪ Data sharing between Java threads is done via parameter passing. Threads
can be created in two ways:
o Extending the Thread Class: A new class is created by extending the Thread class
and overriding the run() method
o Using the Runnable Interface: A class that implements the Runnable interface is
created. This method is more commonly used
Threads
▪ Implicit Threading
▪ With the development of multicore processors, applications now often contain
hundreds or even thousands of threads
▪ Creating and managing such a large number of threads manually is challenging
and prone to errors
▪ As a result, implicit threading, where the compiler handles the creation of
threads, is becoming increasingly popular
Threads
▪ Thread Pools
▪ A multithreaded web server, for instance, creates a new thread for each incoming
request. However, if the number of requests becomes too large, system
resources like CPU time and memory can be exhausted. To prevent this, a thread
pool is created, allowing only a fixed number of threads to be created
▪ When a new request arrives, a thread from the pool is used if available;
otherwise, the system waits for a thread to be released
▪ Using existing threads is faster than creating new ones, which improves system
performance
Threads
▪ Fork Join
▪ The strategy for thread creation is often known as the fork-join model
▪ Fork Join in Java
▪ Java introduced a fork-join library in Version 1.7 of the API that is designed to be
used with recursive divide-and-conquer algorithms such as Quicksort and
Mergesort
Threads
▪ Fork Join
▪ Fork Join in Java
▪ The general recursive algorithm behind Java’s fork-join model is shown below:
Threads
▪ Fork Join
▪ Fork Join in Java
Threads
▪ OpenMP
▪ OpenMP is a set of compiler directives for parallel programming in C, C++, and
Fortran
▪ It supports shared memory architectures and defines parallel sections in code
using directives like #pragma omp parallel
▪ OpenMP can be used with many open-source and commercial compilers on
Linux, Windows, and Mac OS X
Threads
▪ Threading Issues
▪ The fork() and exec() System Calls
▪ In some Unix systems, there are two types of fork() system calls:
o One duplicates all threads
o The other duplicates only the thread that initiated the fork()
▪ The exec() system call duplicates the entire process, including all threads
Threads
▪ Signal Handling
▪ A signal is used in UNIX systems to notify a process that a particular event
has occurred. A signal may be received either synchronously or asynchronously
depending on the source of and the reason for the event being signaled
o Synchronous signals are delivered to the process that caused the event (e.g.,
illegal memory access or division by zero)
o Asynchronous signals are delivered to other processes (e.g., when pressing
<ctrl><C>)
Threads
▪ Signal Handling
▪ Operating systems can send signals to:
o Only one thread within a process (synchronous)
o All threads within a process (e.g., <ctrl><C>)
o Some threads within a process
o A specific thread that is designated to handle all signals
Threads
▪ Thread Cancellation
▪ A thread can be canceled before it completes its execution. For example, if one
thread finds the result needed, the other threads can be canceled. When you
click the stop button while loading a web page, all threads within the process are
canceled.
▪ There are two types of thread cancellation:
o Asynchronous Cancellation: One thread immediately terminates another thread.
o Deferred Cancellation: One thread allows another thread to terminate itself.
Threads