0% found this document useful (0 votes)
0 views

MultiThreading

This document provides an introduction to multithreading, explaining its definition, benefits, and implementation in mobile systems. It covers key concepts such as thread characteristics, scheduling, safety, synchronization, and communication, along with real-world applications and challenges. A case study illustrates how multithreading enhances mobile application performance by allowing concurrent task execution, improving responsiveness, and optimizing resource usage.

Uploaded by

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

MultiThreading

This document provides an introduction to multithreading, explaining its definition, benefits, and implementation in mobile systems. It covers key concepts such as thread characteristics, scheduling, safety, synchronization, and communication, along with real-world applications and challenges. A case study illustrates how multithreading enhances mobile application performance by allowing concurrent task execution, improving responsiveness, and optimizing resource usage.

Uploaded by

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

Introduction to

Multithreading
This presentation will explore the concept of multithreading, its
advantages, and how it is implemented in mobile systems. We will
analyze real-world case studies to demonstrate how multithreading
improves mobile system performance.
What Is A Thread?
A thread is a lightweight process that runs within a program, executing a specific portion
of the code.
It's a separate flow of execution, with its own :( execution happens independently).
Has thread ID, PC, register set, a stack.
By utilizing multithreads programs can divide its workload into smaller units of execution.

Thread Characteristics
Lightweight: Creating a new thread requires less overhead compared to creating a new
process.
Fast context switching: Switching between threads is faster than switching between
processes.
Shared memory: Threads share the same memory space as the parent process.
• THREADS CAN BE CREATED IN 2
Fast context switching: Switching between VARIOUS WAYS:
threads is faster than switching between • EXPLICIT CREATION: USING
processes. THREADING LIBRARIES OR APIS,
LIKE PTHREAD_CREATE() OR
Shared memory: Threads share the same
THREAD.START().
memory space as the parent process.
• IMPLICIT CREATION: SOME
Independent execution: Each thread executes
PROGRAMMING LANGUAGES, LIKE
its own code path. JAVA AND PYTHON CREATE
THREADS AUTOMATICALLY.
-Thread Scheduling: Thread scheduling refers to the algorithmic process of determining when and for how long each thread
should execute on a processor. Thread schedulers, provided by the operating system or runtime environments, allocate CPU time
to threads based on scheduling algorithms like Round-Robin, Priority Scheduling, and First-Come-First-Served. The scheduler's
goal is to optimize resource utilization, fairness, and responsiveness.
-Thread safety: Thread safety ensures that concurrent access to shared resources is safe and efficient. It involves designing
algorithms, data structures, and code in a way that prevents race conditions and ensures correct behavior in a multithreaded
environment. Techniques such as locks, atomic operations, and synchronization primitives are used to achieve thread safety.
Additionally, avoiding shared mutable state or using immutable data structures can simplify thread safety.
Multithreading: Definition and Benefits
Definition Benefits
Multithreading is a programming Multithreading offers several
technique that allows multiple threads advantages, including improved
(or units of execution) to run performance, resource sharing,
concurrently within a single process. It responsiveness, and efficient task
enables efficient utilization of multiple handling. It enhances the efficiency of
processor cores, allowing tasks to be program execution by leveraging
broken down into smaller units that can multiple cores to execute tasks
be executed in parallel. simultaneously, leading to faster
processing times and improved user
experience.
Multithreading Concepts
-Thread Creation: Thread creation involves spawning new threads to perform tasks concurrently. As mentioned earlier, threads can
be created by instantiating thread objects, using function pointers or callbacks, or with task-based programming models. The
creation of threads allows multiple tasks to be executed simultaneously, potentially improving the overall performance and
responsiveness of the application.
-thread Synchronization: In essence, thread synchronization ensures that multiple threads can access shared resources in a
controlled and orderly manner, preventing conflicts, data corruption and race conditions, Synchronization mechanisms such as
locks, semaphores, and monitors are used to control access to shared resources, ensuring that only one thread can access a
resource at a time. These mechanisms prevent data races and maintain data integrity.
-thread Communication: Thread communication involves exchanging data between threads. Threads often need to cooperate and
exchange information to accomplish tasks collectively. Communication mechanisms such as pipes, sockets, and shared memory can
be used. Pipes and sockets facilitate communication between threads running on different machines or processes. Shared memory
allows threads running within the same process to share data directly.
Multithreading in Mobile
Systems
Application in Mobile Devices
Modern smartphones utilize multithreading to improve the
responsiveness of applications, particularly in areas like
gaming, media processing, and background services. This
allows for smooth user interactions and efficient task
execution.

Battery Efficiency
By distributing tasks across multiple cores efficiently,
multithreading can improve power consumption and prevent
CPU overheating, extending battery life and enhancing
device performance.
Case Study: Multithreading in Parallel Computing
Mobile Systems
Problem
Mobile applications are increasingly expected to handle multiple tasks simultaneously, such as syncing data with cloud
servers, downloading files, and sending notifications, all while ensuring that the user interface (UI) remains responsive.
Without multithreading, running these tasks sequentially can result in sluggish performance, freezing of the UI, and longer
waiting times for the user.
Solution
By applying multithreading, the application can offload resource-intensive tasks like data synchronization, downloading, and
notification processing to background threads, while maintaining a smooth and responsive UI in the main thread. The app
can assign multiple threads to handle different tasks concurrently.

Implementation
Using OpenMP, we can simulate this case study where the app handles data syncing, downloading, and notification
processing in a multithreaded environment. The #pragma omp parallel sections directive is used to create parallel sections
where different tasks can run concurrently.
Benefits and Challenges of
Multithreading
Benefits
Multithreading offers several benefits, including improved
responsiveness, increased efficiency, enhanced user experience,
and scalability. By offloading tasks to background threads, the app
ensures that the UI thread remains responsive, allowing users to
interact smoothly with the app while tasks are completed in the
background.

Challenges
While multithreading offers significant advantages, it also presents
challenges, such as thread synchronization, overhead, and battery
usage. Ensuring proper synchronization between threads can be
difficult, and managing multiple threads introduces some overhead,
which needs to be carefully considered.
Question:You are writing a program to simulate a morning routine using
multithreading. You have three tasks: eating breakfast (5 seconds), drinking coffee
(4 seconds), and studying (3 seconds). Each task should run concurrently using
threads. After starting all threads, print the number of active threads, a list of
active thread objects, and the current performance counter.
Write a Python program using the threading and time modules to achieve this?

SOLUTION CODE
#include <stdio.h>
#include <pthread.h> int main() {
pthread_t t1, t2, t3;
#include <unistd.h>
struct timespec start_time;
#include <time.h> clock_gettime(CLOCK_MONOTONIC,
&start_time); // get start time
void* eat_breakfast(void* arg) {
sleep(5); // simulate eating pthread_create(&t1, NULL, eat_breakfast,
breakfast NULL);
printf("You eat breakfast\n"); pthread_create(&t2, NULL, drink_coffee, NULL);
return NULL; pthread_create(&t3, NULL, study, NULL);
}
// Optionally wait for threads to finish
pthread_join(t1, NULL);
void* drink_coffee(void* arg) { pthread_join(t2, NULL);
sleep(4); // simulate drinking pthread_join(t3, NULL);
coffee
printf("You drank coffee\n"); // Get performance counter
return NULL; struct timespec end_time;
} clock_gettime(CLOCK_MONOTONIC, &end_time);
double elapsed = (end_time.tv_sec -
start_time.tv_sec)
void* study(void* arg) { + (end_time.tv_nsec -
sleep(3); // simulate studying start_time.tv_nsec) / 1e9;
printf("You finish studying\n");
return NULL; printf("All tasks completed.\n");
} printf("Elapsed time: %.2f seconds\n", elapsed);

return 0;
}
THANK YOU

You might also like