SlideShare a Scribd company logo
Multithreading
and
concurrency
SHYMA’A QADOOM
Concurrency
• is the tendency for things
to happen at the same time
in a system. ... Figure 1:
Example of concurrency at
work: parallel activities that
do not interact have simple
concurrency issues. It is when
parallel activities interact or
share the same resources that
concurrency issues become
important.
• Concurrent
• Two queues and one
coffee machine.
• Parallel
• Two queues and two
coffee machines.
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
What are threads in
C#?
A thread is defined as the
execution path of a program. Each
thread defines a unique flow of
control. If your application involves
complicated and time-consuming
operations, then it is often helpful
to set different execution paths or
threads, with each thread
performing a particular job.
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
Multithreading and concurrency.pptx
In the main thread in the “Main” method we create three other threads, methods that
display characters in the console and you can see that at the same time characters
from the “Main” method are displaying, i.e. our program is multithreaded.
You can also wait for the thread to finish working, then you need to add the Join()
method
You can see the differences that the threads are executing now in turn.
We can also put the thread to sleep for a while, let’s put the thread y to sleep:
Multithreading and concurrency.pptx
Synchronization - lock
Keyword
Since we got inconsistent result, we need to find a way to
programmatically enforce synchronized access to the shared
resources. The System.Threading provides a number of
synchronization types as we'll see in the following sections.
In this section, we're going to use lock keyword for the synchronized
access to shared resources. The lock allows us to define a scope of
code that must be synchronized between threads so that incoming
threads cannot interrupt the current thread. To use the lock, we
need to specify a token that must be acquired by a thread to enter
inside the scope which has been locked. So, when we want to lock
down a private instance method, we can simply pass in an object
reference to the current type:
Synchronization - Mutex
A Mutex is like the lock, however, it can work
across multiple processes. But it is slower than
the lock.
Mutex allows us to call the WaitOne() method to
lock and ReleaseMutex() to unlock. Note that
a Mutex can be released only from the same
thread which obtained it. The primary use for a
cross-process Mutex is to ensure that only one
instance of a program can run at a time.
Synchronization -
Semaphore
A Semaphore restricts the number of simultaneous
users of the shared resources up to a maximum
number. It ensures not more than a specific
number of concurrent threads, which can access a
resource.
A Semaphore is like a football team, it has a
certain capacity Once it is full, no more player can
enter and other players wait outside in the form of
queue. For each player that leaves, another one
enters from the head of the queue.
• On a computer, the
operating system loads and
starts applications. Each
application or service runs as
a separate process on the
machine. The following image
illustrates that there are quite
a few processes running than
there are actual applications
running in the system. Many
of these processes are
background operating system
processes that are started
automatically when the OS
loads.
• Obtaining Current Thread
Information
• To illustrate the basic use
of the Thread type, suppose
you have a console
application in which the
Current Thread property
retrieves a Thread object
that represents the currently
executing thread.
Multithreading and concurrency.pptx
Tasks
• The thread is a low-level tool designed to provide concurrency and
therefore has some limitations.
• It is easy to pass data to it, but it is not easy to get a return value from it.
• You can not tell a thread to execute another thread when it ends. Instead,
it is necessary to use the Join() method, which means blocking the thread at
that time.
• 3. Direct use of threads also worsens performance.
The Task class is the solution to all these problems.
Go to demos…
In this example, the WriteY() method will be executed immediately, then we
have to wait 3 seconds to execute the next threads, but the time must be given
in milliseconds.
While there is a problem with
threads, look at the example below:
At the same time, two threads are executed that start the same method,
this will display the word “Done” twice in the console.
How can you check the security of threads? The keyword “lock” is used
for this. So, we will secure our threads in the example below.
The solution used in the above example guarantees that one block can
be put on one thread at a time, which means that the word “Done” will
be displayed once.
Thank you..
Have a nice day!

More Related Content

PDF
Sync, async and multithreading
Tuan Chau
 
PDF
.Net Threading
Erik Ralston
 
PDF
Concurrency and parallel in .net
Mohammad Hossein Karami
 
PPTX
C# Thread synchronization
Prem Kumar Badri
 
PPT
Threads and Synchronization in c#
Rizwan Ali
 
PPT
Threads And Synchronization in C#
Rizwan Ali
 
PPTX
Threads and synchronization in C# visual programming.pptx
azkamurat
 
PPS
11 iec t1_s1_oo_ps_session_16
Niit Care
 
Sync, async and multithreading
Tuan Chau
 
.Net Threading
Erik Ralston
 
Concurrency and parallel in .net
Mohammad Hossein Karami
 
C# Thread synchronization
Prem Kumar Badri
 
Threads and Synchronization in c#
Rizwan Ali
 
Threads And Synchronization in C#
Rizwan Ali
 
Threads and synchronization in C# visual programming.pptx
azkamurat
 
11 iec t1_s1_oo_ps_session_16
Niit Care
 

Similar to Multithreading and concurrency.pptx (20)

PPTX
Threading
abhay singh
 
PPTX
.NET: Thread Synchronization Constructs
Sasha Kravchuk
 
PPT
Intro To .Net Threads
rchakra
 
PPT
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
PPTX
Threading in C#
Medhat Dawoud
 
PDF
Threading in c#
gohsiauken
 
PPTX
concurrency_c#_public
Paul Churchward
 
PPT
Threads c sharp
Deivaa
 
PPT
Threads-CShharp.pptsdfsdfsdgdgsdfasdfsdfasdf
SonamPandey35
 
PPTX
multi threading
Yaswanth Babu Gummadivelli
 
PPTX
Scheduling Thread
MuhammadBilal187526
 
PPT
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
PPTX
MERIMeeting du 27 mai 2014 - Parallel Programming
Olivier NAVARRE
 
PPT
Multithreading Presentation
Neeraj Kaushik
 
PDF
Intake 38 12
Mahmoud Ouf
 
PPTX
.NET Multithreading/Multitasking
Sasha Kravchuk
 
PDF
Threading in c_sharp
Tiago
 
PPTX
C# Async/Await Explained
Jeremy Likness
 
PPTX
Multi-Threading
Robert MacLean
 
PPTX
Multi core programming 2
Robin Aggarwal
 
Threading
abhay singh
 
.NET: Thread Synchronization Constructs
Sasha Kravchuk
 
Intro To .Net Threads
rchakra
 
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
Threading in C#
Medhat Dawoud
 
Threading in c#
gohsiauken
 
concurrency_c#_public
Paul Churchward
 
Threads c sharp
Deivaa
 
Threads-CShharp.pptsdfsdfsdgdgsdfasdfsdfasdf
SonamPandey35
 
Scheduling Thread
MuhammadBilal187526
 
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
MERIMeeting du 27 mai 2014 - Parallel Programming
Olivier NAVARRE
 
Multithreading Presentation
Neeraj Kaushik
 
Intake 38 12
Mahmoud Ouf
 
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Threading in c_sharp
Tiago
 
C# Async/Await Explained
Jeremy Likness
 
Multi-Threading
Robert MacLean
 
Multi core programming 2
Robin Aggarwal
 
Ad

Recently uploaded (20)

PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Doc9.....................................
SofiaCollazos
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Ad

Multithreading and concurrency.pptx

  • 2. Concurrency • is the tendency for things to happen at the same time in a system. ... Figure 1: Example of concurrency at work: parallel activities that do not interact have simple concurrency issues. It is when parallel activities interact or share the same resources that concurrency issues become important.
  • 3. • Concurrent • Two queues and one coffee machine. • Parallel • Two queues and two coffee machines.
  • 6. What are threads in C#? A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
  • 26. In the main thread in the “Main” method we create three other threads, methods that display characters in the console and you can see that at the same time characters from the “Main” method are displaying, i.e. our program is multithreaded. You can also wait for the thread to finish working, then you need to add the Join() method You can see the differences that the threads are executing now in turn. We can also put the thread to sleep for a while, let’s put the thread y to sleep:
  • 28. Synchronization - lock Keyword Since we got inconsistent result, we need to find a way to programmatically enforce synchronized access to the shared resources. The System.Threading provides a number of synchronization types as we'll see in the following sections. In this section, we're going to use lock keyword for the synchronized access to shared resources. The lock allows us to define a scope of code that must be synchronized between threads so that incoming threads cannot interrupt the current thread. To use the lock, we need to specify a token that must be acquired by a thread to enter inside the scope which has been locked. So, when we want to lock down a private instance method, we can simply pass in an object reference to the current type:
  • 29. Synchronization - Mutex A Mutex is like the lock, however, it can work across multiple processes. But it is slower than the lock. Mutex allows us to call the WaitOne() method to lock and ReleaseMutex() to unlock. Note that a Mutex can be released only from the same thread which obtained it. The primary use for a cross-process Mutex is to ensure that only one instance of a program can run at a time.
  • 30. Synchronization - Semaphore A Semaphore restricts the number of simultaneous users of the shared resources up to a maximum number. It ensures not more than a specific number of concurrent threads, which can access a resource. A Semaphore is like a football team, it has a certain capacity Once it is full, no more player can enter and other players wait outside in the form of queue. For each player that leaves, another one enters from the head of the queue.
  • 31. • On a computer, the operating system loads and starts applications. Each application or service runs as a separate process on the machine. The following image illustrates that there are quite a few processes running than there are actual applications running in the system. Many of these processes are background operating system processes that are started automatically when the OS loads.
  • 32. • Obtaining Current Thread Information • To illustrate the basic use of the Thread type, suppose you have a console application in which the Current Thread property retrieves a Thread object that represents the currently executing thread.
  • 34. Tasks • The thread is a low-level tool designed to provide concurrency and therefore has some limitations. • It is easy to pass data to it, but it is not easy to get a return value from it. • You can not tell a thread to execute another thread when it ends. Instead, it is necessary to use the Join() method, which means blocking the thread at that time. • 3. Direct use of threads also worsens performance. The Task class is the solution to all these problems.
  • 36. In this example, the WriteY() method will be executed immediately, then we have to wait 3 seconds to execute the next threads, but the time must be given in milliseconds.
  • 37. While there is a problem with threads, look at the example below: At the same time, two threads are executed that start the same method, this will display the word “Done” twice in the console.
  • 38. How can you check the security of threads? The keyword “lock” is used for this. So, we will secure our threads in the example below. The solution used in the above example guarantees that one block can be put on one thread at a time, which means that the word “Done” will be displayed once.
  • 39. Thank you.. Have a nice day!