0% found this document useful (0 votes)
18 views30 pages

Lecture 7 - Multithreaded - Asynchronous - and Parallel Programming

The document discusses multithreaded, asynchronous, and parallel programming, highlighting their importance in improving application performance and responsiveness during heavy computation or I/O operations. It covers key concepts such as threads, thread pools, and the Task Parallel Library (TPL), along with challenges like race conditions and deadlocks. Additionally, it explains the differences between synchronous and asynchronous programming, and the benefits of using parallel programming to maximize resource utilization on multi-core processors.

Uploaded by

m09221011
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)
18 views30 pages

Lecture 7 - Multithreaded - Asynchronous - and Parallel Programming

The document discusses multithreaded, asynchronous, and parallel programming, highlighting their importance in improving application performance and responsiveness during heavy computation or I/O operations. It covers key concepts such as threads, thread pools, and the Task Parallel Library (TPL), along with challenges like race conditions and deadlocks. Additionally, it explains the differences between synchronous and asynchronous programming, and the benefits of using parallel programming to maximize resource utilization on multi-core processors.

Uploaded by

m09221011
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/ 30

Multithreaded, Asynchronous,

and Parallel Programming


Abdulmottaleb Elabour

1
What Are We Solving?
• Problem: Applications freeze or perform slowly under heavy computation or I/O
operations.
• Solution: Use concurrency patterns to improve responsiveness and performance.

2
‫عبد المطلب العبور‬
Multithreaded Programming
• Multi-threaded programming involves creating and managing multiple threads within a

single application. This allows the application to perform multiple tasks concurrently,

improving performance and responsiveness, especially on multi-core processors.

• Involves manually creating and managing threads.

• Best suited for scenarios where you need to manage how and when threads are

created and executed."

3
‫عبد المطلب العبور‬
Multithreaded Programming
Key Concepts

• Thread: The smallest unit of a process that can be scheduled for execution. In C#,
you can create and manage threads using the Thread class.
• Thread Pool: A pool of worker threads managed by the .NET Framework. It handles
thread creation and management, which helps improve performance and resource
management.
• Task: A higher-level abstraction over threads. Tasks are part of the Task Parallel Library
(TPL) and provide a more efficient and easier way to work with asynchronous
operations.

4
‫عبد المطلب العبور‬
Multithreaded Programming
Scenario 1: No dependencies between thread results

5
‫عبد المطلب العبور‬
Multithreaded Programming
Scenario 2: Dependencies between thread results

6
‫عبد المطلب العبور‬
Challenges Multithreaded Programming
• Race Conditions: Occur when two threads access shared data and try to change it
simultaneously.
• Deadlocks: Occur when two or more threads are waiting for each other to release
resources, resulting in a standstill.
• Resource Starvation occurs when a thread is continually denied access to resources
and can’t proceed with its work.

7
‫عبد المطلب العبور‬
Asynchronous Programming
• Asynchronous programming in .NET allows a program to perform tasks without
blocking the main thread, enabling the program to remain responsive.
• This is particularly useful for tasks that might take some time, such as file I/O
operations, network requests, or any other long-running processes.

8
‫عبد المطلب العبور‬
Asynchronous Programming

9
‫عبد المطلب العبور‬
Key Concepts of Asynchronous Programming in .NET:
1. Async and Await: These are keywords used to define asynchronous methods.
• async: Used to declare a method as asynchronous.
• await: Used to pause the execution of an async method until the awaited task
completes.
2. Tasks: Represents an asynchronous operation. The Task class is used to handle and
control these operations

10
‫عبد المطلب العبور‬
Thread Pool
• A collection of threads managed by .NET to perform background tasks. Instead of
creating a new thread every time an asynchronous task is performed, .NET uses
threads from this pool to optimize performance.

• When you use async and await, the method doesn't block the main thread.
Instead, it runs on a thread from the thread pool. Once the awaited task
completes, it returns to the context it was called from, which is often the main
thread.

11
‫عبد المطلب العبور‬
Thread Pool
public async Task<int> FetchDataCountAsync()
{
var products = productService.GetAll();
var productLength = products.length();
// 3. Call an asynchronous method to get all categories.
// This does not block the current thread.
// The control returns to the caller until this task is completed.
var categories = await catService.GetAll();
// The method pauses here until catService.GetAll() completes.
// Once completed, the result is assigned to the 'categories' variable.
// 4. Calculate the length of the categories array.
// This runs on the current thread after the await completes.
var catLength = categories.Length();
// 5. Call an asynchronous method to get all limits.
// This does not block the current thread.
// The control returns to the caller until this task is completed.
var limits = await limitService.getAll();
// The method pauses here until limitService.getAll() completes.
// Once completed, the result is assigned to the 'limits' variable.
// 6. Calculate the length of the limits array.
// This runs on the current thread after the await completes.
var limitLength = limits.length();
// 7. This runs on the current thread.
return productLength + catLength + limitLength;
} 12
‫عبد المطلب العبور‬
Asynchronous vs Synchronous
• In .NET, synchronous methods typically do not directly interact with the thread
pool unless they explicitly use it, such as via Task.Run or
ThreadPool.QueueUserWorkItem.
• By default, synchronous methods run on the current thread that calls them.
However, you can use the thread pool to run synchronous methods in a background
thread, thus freeing up the main thread.

13
‫عبد المطلب العبور‬
Asynchronous vs Synchronous
public int FetchDataSync()
{
var products = productService.GetAll();
var productLength = products.length();
// 3. Call to get all categories.
// This runs on the current thread and blocks until it completes.
var categories = catService.GetAll();
// 4. Calculate the length of the categories array.
// This runs on the current thread after the previous line
completes.
var catLength = categories.Length();
// 5. Call to get all limits.
// This runs on the current thread and blocks until it completes.
var limits = limitService.getAll();
// 6. Calculate the length of the limits array.
// This runs on the current thread after the previous line
completes.
var limitLength = limits.length();

// 7. This runs on the current thread after the previous lines


complete.
return productLength + catLength + limitLength;
} 14
‫عبد المطلب العبور‬
Asynchronous vs Synchronous

15
‫عبد المطلب العبور‬
Task Parallel Library (TPL)
The Task Parallel Library (TPL) is a set of public types and APIs in
the System.Threading and System.Threading.Tasks namespaces. The purpose of the TPL is to make developers
more productive by simplifying the process of adding parallelism and concurrency to applications.

16
‫عبد المطلب العبور‬
Wait for all tasks to complete and get their results

‫ عبد المطلب العبور‬17


Returning Completed Tasks
You have several tasks and need to
respond to just one of them that’s
completing.
You’ll encounter this problem most
commonly when you have multiple
independent
attempts at an operation, with a
first-one-takes-all kind of structure

‫ عبد المطلب العبور‬18


Returning Completed Tasks
You have several tasks and need to
respond to just one of them that’s
completing.
You’ll encounter this problem most
commonly when you have multiple
independent
attempts at an operation, with a
first-one-takes-all kind of structure

‫ عبد المطلب العبور‬19


Parallel Programming
• Parallel Programming in C# helps us divide a task into different parts and
work those parts simultaneously.
• The main benefit of parallelism is saving time. Time is saved by maximizing the use of
computer resources.
• The idea is that if the computer allows the use of multi-threading, we can use these
threads when we have a task to solve. So, instead of underusing our processor using a
single thread, we can use as many threads as we can to speed up the processing of the
task.
• Parallel programming should be used any time you have a fair amount of computation
work that can be split up into independent chunks.

‫ عبد المطلب العبور‬20


Why do we need Task Parallel Library in C#?
We can t expect our sequential program to run faster
on the new processors as we know the processor
technology advances means the focus is on Multicore
processors.
Today s desktop typically has 4 cores but the latest
experimental multi core chips have up to 1000 cores.
Using the Task Parallel Library (TPL), we can express
the parallelism in the existing sequential code, which
means we can express the code as a Parallel task,
which will be run concurrently on all the available
processors.

‫ عبد المطلب العبور‬21


Why do we need Task Parallel Library in C#?
Parallel Programming in C# is a type of programming in which many calculations or the
execution of processes are carried out simultaneously. The Points to Remember while
working with Parallel Programming:

1. The Tasks must be independent. The order of the execution does not matter.
2. The order of the execution does not matter

‫ عبد المطلب العبور‬22


Why do we need Task Parallel Library in C#?
Data Parallelism:
In Data Parallelism, we have a collection of values and we want to use the same operation
on each of the elements in the collection. The examples will be to filter the elements of an
array in parallel or find the inverse of each matrix in a collection. This means each process
does the same work on unique and independent pieces of data.
Example:
• Parallel.For
• Parallel.ForEach

‫ عبد المطلب العبور‬23


Parallel.For Example
Output:

‫ عبد المطلب العبور‬24


What is the difference between the Parallel For loop and Standard
C# for loop?

The main difference between the Parallel For loop and the standard C# for loop is as follows:
1.In the case of the standard C# for loop, the loop is going to run using a single thread whereas, in the
case of the Parallel For loop, the loop is going to execute using multiple threads.
2.The second difference is that, in the case of the standard C# for loop, the loop is iterated in sequential
order whereas, in the case of the Parallel For loop, the order of the iteration is not going to be in
sequential order.

‫ عبد المطلب العبور‬25


What is the difference between the Parallel For loop and Standard
C# for loop?

‫ عبد المطلب العبور‬26


The Degree of Parallelism in C#:
Using the Degree of parallelism we can specify the
maximum number of threads to be used to execute the
program.
Following is the syntax to use ParallelOptions class
with a Degree of Parallelism.
The MaxDegreeOfParallelism property affects the
number of concurrent operations run by Parallel
method calls that are passed this ParallelOptions
instance. A positive property value limits the number of
concurrent operations to the set value. If it is 1 , there
is no limit on the number of concurrently running
operations.

‫ عبد المطلب العبور‬27


The Degree of Parallelism in C#:
One thing to keep in mind is
that each parallel task may run
on a different thread, so any
shared state must be
protected.
The lock statement is used here
to ensure that only one thread
at a time can access the
critical section where the
shared variable 'sum' is
updated.
This prevents race conditions
and ensures thread safety.

‫ عبد المطلب العبور‬28


PLINQ – Parallel LINQ
PLINQ automatically parallelizes local LINQ queries. PLINQ has the advantage of being easy to use in that it
offloads the burden of both work partitioning and result collation to .NET.
To use PLINQ, simply call AsParallel() on the input sequence and then continue the LINQ query as usual. The following
query calculates the prime numbers between 3 and 100,000, making full use of all cores on the target machine:

‫ عبد المطلب العبور‬29


PLINQ – Parallel LINQ

PLINQ execution model


‫ عبد المطلب العبور‬30

You might also like