SlideShare a Scribd company logo
1 of 39
Download to read offline
Concurrency AND Parallel
Speaking of concurrency, we have to start talking about
threads. Ironically, the reason behind implementing threads
was to isolate programs from each other. Back in the early
days of Windows, versions 3.* used cooperative multitasking.
This meant that the operating system executed all the
programs on a single execution loop, and if one of those
programs hung, every other program and the operating
system itself would stop responding as well and then it
would be required to reboot the machine to resolve this
problem.
Why Concurrency
Simply using multiple threads in a program is not a very complicated
task. If your program can be easily separated into several
independent tasks, then you just run them in different threads
However, usually real world programs require some interaction
between these threads, such as exchanging information to coordinate
their work
Dealing with this shared state is the root of almost every problem
related to parallel programming
What’s the problem?
Using locks
How Solve Problem
It is important to always release a lock after acquiring it. Always put the
code for releasing a lock into the finally block of the try / catch statement,
otherwise any exception thrown before releasing this lock would leave the
ReaderWriterLock object in a locked state, preventing any further access to
this lock.
Lock localization
The first thing to take into account when writing parallel
code is to lock as little code as possible, and ensure that
the code inside the lock runs as fast as possible This
makes it less deadlock-prone and scale better with the
number of CPU cores
Optimization strategy
Shared data minimization
It is a common situation when we lock over the whole
collection every time we write into it, instead of thinking
and lowering the amount of locks and the data being
locked. Organizing concurrent access and data storage
in a way that it minimizes the number of locks can lead
to a significant performance increase
Parallel processing
Doing lots of work by dividing it up among multiple threads that run
concurrently
Parallel processing is one type of multithreading, and
multithreading is one type of concurrency
Asynchronous programming
A form of concurrency that uses futures or callbacks to avoid unnecessary
threads
the thread that starts the operation is free to do
When the operation completes, it notifies its future or invokes its callback or
event to let the application know the operation is finished.
Introduction to Asynchronous Programming
Asynchronous programming has two primary benefits
• asynchronous programming enables responsiveness. can remain
responsive to user input while it’s working
• asynchronous server application can usually scale an order of
magnitude better than sync
Both benefits of asynchronous programming derive from the same
underlying aspect:
frees up a thread
One of the most essential tasks when writing parallel code is to divide
your program into subsets that will run in parallel and communicate
between each other. Sometimes the task naturally divides into separate
pieces, but usually it is up to you to choose which parts to make
parallel. Should we use a small number of large tasks, many small
tasks, or maybe large and small tasks at the same time?
Understanding Parallelism
Granularity
what is a thread’s cost for the operating system and CPU? What
number of threads is optimal?
In Windows and in the 32-bit mode, the maximum number of
threads in your process is restricted by the virtual address space
available, which is two gigabytes. A thread stack’s size is one
megabyte, so we can have maximum 2,048 threads. In a 64-bit
OS for a 32-bit process, it should be 4,096
The number of threads
Please be aware that if we
run this in 64-bit mode, the
program will exhaust system
resources and might cause
the OS to hang
The first reason is that when we run a 32-bit process on 64-bit
Windows, a thread will have a 64-bit stack as well, and the actual
stack allocation will be 1 MB + 256 KB of the 64-bit stack.
The second reason is that our process is limited to 2 GB of the
address space. If we want to use more, we have to specify a special
flag, IMAGE_FILE_LARGE_ADDRESS_AWARE, for our program, which
is set using the /LARGEADDRESSAWARE linker option. We cannot
set this flag directly in Visual Studio, but we are able to use a tool
called EditBin.exe, which is included in Visual Studio installation.
we getting 1,522 threads while we expected to get about 4,000
when we compiled our program in 32-bit mode?
Thread Pool
creating a thread is quite an expensive operation. In addition to this,
creating more and more threads is not efficient. To make asynchronous
operations easier, in Common Language Runtime there is a thread pool
There are two types of threads inside the thread pool:
o worker threads
o I/O threads
There is one thread pool per process.
Task Parallel Library (TPL)
This chart shows that when we reduce task size, we increase performance until some point. Then the task
size becomes small enough to achieve full CPU workload. Making tasks smaller becomes ineffective due to
an overall task overhead increase.
if we need multiple threads to add some item to a collection, we
cannot just call the Add method of a shared instance of the List<T>
type. It will lead to unpredictable results, and most probably the
program will end up throwing a weird exception
Using Concurrent Data Structures
thread contention
it can significantly decrease your program
performance
Concurrent collections
This approach is called coarse-grained locking ,A complicated, but an efficient,
approach is to use fine-grained locking, so we can provide an exclusive access
only to the parts of the collection that are in use. For example, if the underlying
data storage is an array, we can create multiple locks that will cover the
corresponding array parts. This approach requires determining the required lock
first, but it will also allow a non-blocking access to the different parts of the array.
This will use locks only when there is a concurrent access to the same data. In
certain scenarios, the performance difference will be huge
ConcurrentDictionary
Implementation details The Tables class contains the following most important fields:
m_buckets: This is an array of buckets; each of the buckets
contains a singly-linked list of nodes with dictionary data.
m_locks: This is an array of locks; each lock provides
synchronized access to one or more buckets.
m_countPerLock: This is an array of counters; each counter
contains a total number of nodes that are protected by the
corresponding lock. For example, if we look at the previous
scheme, where the first lock protects the first two buckets, the
m_countPerLock[0] element will contain the value of 5.
m_comparer: This is an IEqualityComparer<TKey> object
that contains the logic for calculating the hash value of a key
object.
The entire ConcurrentDictionary state is placed in a
separate Tables class instance in the m_tables field.
This makes it possible to have an atomic state
change operation for the dictionary with the help of
the compare-and-swap (CAS) operations.
API framework
Each worker thread starts running our code and waits two
seconds doing nothing. Then, they return the response. As
we may recall from the previous chapters, thread pool
worker threads are a limited resource, and when we start
issuing 1,000 concurrent requests in a short time, all the
worker threads become occupied running Thread.Sleep. At
the same time
Task.Delay uses a timer object under the hood. This allows
an ASP.NET worker thread to start a wait operation, and
then to return to the application pool and process some
other requests. When two seconds pass, the timer posts a
continuation callback to an available ASP.NET thread pool
worker thread. This allows the application to process more
user requests, since worker threads are not blocked. So,
this timer object helps our application to remain fast and
scalable.
I/O and CPU-bound tasks
there are tasks related to input/output processes, such as reading or writing a file, issuing a network request, or even
performing a query against a database. These operations usually take much more time compared to CPU bound work. So
does this mean that our worker threads will be locked for a longer time and the application will fail to scale?
When we mention a file or network request, we know that there are physical devices such as disks and network cards that
actually execute these operations. These devices have controllers, and a controller in this context means a micro-
computer with its own CPU. To perform an I/O bound task, we do not need to waste the main CPU’s time, it is enough to
give all the required data to the I/O device controller, and it will perform the I/O operation and return the results with the
help of a device driver.
To communicate with the I/O devices, Windows uses a special object called I/O Completion Port (or IOCP). It behaves
pretty much like a timer, but the signals are coming from the I/O devices and not from the internal clock. This means that,
while an I/O operation is in progress, we can reuse the ASP.NET worker thread to serve other requests, and thus achieve
good scalability.
Notice a new entity called the I/O thread in the preceding diagram. There is a separate smaller pool of I/O threads inside this
.NET thread pool. The I/O threads are not different from the usual worker threads, but they are being used only to execute
continuation callbacks for asynchronous I/O operations. If we use general worker threads for this purpose, it can happen that
there are no worker threads available and we cannot complete the I/O operation, which in turn will lead to deadlocks. Using a
separate thread pool will help to prevent this, but we also need to be very careful not to cause I/O threads starvation
Real and fake asynchronous I/O operations
only the numbers two and three writes are
asynchronous.
if we do not specify the correct options for the file API we use, the
file will provide us with the wrong kind of asynchrony that uses
worker threads for the I/O process and thus is not scalable
This workflow is even worse than the usual synchronous code, because there is an additional performance overhead related
to running this part of the operation on a different worker thread. We end up wasting worker thread for the entire time of the
I/O operation anyway, and this is fake asynchronous I/O. It is actually a CPU-bound operation that will affect the scalability
and performance of your application.
Technically, the most efficient way will be to run such work
synchronously and scale horizontally by adding more and more
servers to be able to handle increasing load. Nevertheless, it can
happen that this CPU-bound work is not the only responsibility of a
server application.
if there is a long running operation, a web application registers it
into some data store, returns a unique identifier of this operation
to the client, and posts this operation to a special queue. Then there
is a separate pool of worker processes that monitor this queue, get
tasks from them, process them, and write results to a data store.
When the client arrives next time, the web application checks
whether the task has been already completed by any worker and if it
has, the application returns the result to the client.
CPU-bound tasks and queues
While a server application in general has to be scalable before everything
else, a client application is different. It is usually intended to run for one
user on one computer, and thus the user expects it to run fast and not
cause troubles for the other applications running in the system. While the
second part is usually handled by the operating system, the application’s
performance in getting things done and reacting to user input is crucial for a
positive user experience.
we have to learn how the UI works and the UI threading architecture.
UI threads and message loops
Concurrency in the User Interface
Troubleshooting Parallel
Programs
A concurrent program like any usual program can contain programming errors
that could lead to incorrect results. However, concurrency usually leads
programs to become more complicated, causing errors to be trickier and
harder to find. There are typical problems related to concurrent shared state
access conditions and deadlocks, but there are many other kinds of
problems specific to concurrent programs.
This is one more problem type, not strictly related to concurrent programming, but much more common with it.
In computer programming jargon, a heisenbug is a software bug that seems to disappear or alter its behaviour when one
attempts to study it. The term is a pun on the name of Werner Heisenberg, the physicist who first asserted the observer effect
of quantum mechanics, which states that the act of observing a system inevitably alters its state.
These problems are usually extremely hard to reproduce and debug, since they usually appear in some special conditions such
as high user load, or some specific events timing, and more. This is the kind of bug which you will inevitably meet while
developing concurrent applications.
Heisenbugs
Writing tests: This is a very important step that can dramatically
reduce bugs in your code. With these tests, it is possible to detect
problems right after writing the code, or after deploying your
application into a test environment.
Debugging: Visual Studio has specific features and tools to make
debugging concurrent applications easier.
Performance measurement and profiling: This is one more very
important step that can help to detect whether your program
spends too much time switching between threads or blocking
them instead of doing its job.
Test Async Method
Mocking asynchronous dependencies can be a bit awkward at first.
It’s a good idea to at least test how your methods respond to
synchronous, synchronous errors, and asynchronous success
When testing asynchronous code, deadlocks and race conditions may surface more often than
when testing synchronous code
Do not forget to await the task returned by ThrowsAsync, If you forget the await and ignore
the compiler warning, your unit test will always silently succeed regardless of your method’s
behavior
--------------------------------------------------------------------------------------------------------
It’s better to test for an exception thrown at a specific point rather than testing for an
exception at any time during the test. Instead of ExpectedException, use ThrowsAsync
Async Void
Avoid async void! It is possible to have an async method return void, but you should only do
this if you’re writing an async event handler. A regular async method without a return value
should return Task, not void.
parallelism
• Data parallelism is when you have a bunch of data
items to process, and the processing of each piece of
data is mostly independent from the other pieces
• Task parallelism is when you have a pool of work to
do, and each piece of work is mostly independent
from the other pieces
data parallelism
Parallel.ForEach
AsParallel
task parallelism
Parallel.Invoke
data parallelism and task parallelism are similar, “processing data” is a kind of “work”
Many parallelism problems can be solved either way
Reactive Programming (Rx)
Reactive programming has a higher learning curve than other forms of
concurrency, and the code can be harder to maintain unless you keep
up with your reactive skills
Reactive programming enables you to treat a stream of events like a
stream of data.
Reactive programming is based on the notion of observable streams
When you subscribe to an observable stream, you’ll receive any
number of data items (OnNext), and then the stream may end with a
single error (OnError) or “end of stream” notification (OnCompleted)
The example code starts with a counter running off a periodic timer (Interval) and adds a timestamp to each
event (Timestamp). It then filters the events to only include even counter values (Where), selects the timestamp
values (Timestamp), and then as each resulting timestamp value arrives, writes it to the debugger (Subscribe)
More read : TPL Dataflows
Introduction to Multithreaded Programming
There is almost no need for you to ever create a new thread yourself.
The only time you should ever create a Thread instance is if you need
an STA thread for COM interop.
A thread is a low-level abstraction.
you need to aggregate the results.
Examples of aggregation are summing up values or finding
their average.
Parallel Aggregation
Concurrency and parallel in .net

More Related Content

What's hot

.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13aminmesbahi
 
.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 16.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 16aminmesbahi
 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket basedMukesh Tekwani
 
.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8aminmesbahi
 
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESOBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESsuthi
 
MOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufMOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufGaurav Bhardwaj
 
Concurrency in c#
Concurrency in c#Concurrency in c#
Concurrency in c#RezaHamidpour
 
.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7aminmesbahi
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMahmoudOHassouna
 
.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9aminmesbahi
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllersMahmoudOHassouna
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF CoreMahmoudOHassouna
 
Android AsyncTask Tutorial
Android AsyncTask TutorialAndroid AsyncTask Tutorial
Android AsyncTask TutorialPerfect APK
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12Sisir Ghosh
 
.Net Threading
.Net Threading.Net Threading
.Net ThreadingErik Ralston
 
DATA BASE MANAGEMENT SYSTEM - SHORT NOTES
DATA BASE MANAGEMENT SYSTEM - SHORT NOTESDATA BASE MANAGEMENT SYSTEM - SHORT NOTES
DATA BASE MANAGEMENT SYSTEM - SHORT NOTESsuthi
 
25250716 seminar-on-ajax text
25250716 seminar-on-ajax text25250716 seminar-on-ajax text
25250716 seminar-on-ajax textKamleshh Chandnani
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletPayal Dungarwal
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databasePayal Dungarwal
 
Command pattern in java
Command pattern in javaCommand pattern in java
Command pattern in javaRakibAhmed0
 

What's hot (20)

.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
 
.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 16.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 16
 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket based
 
.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8
 
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESOBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
 
MOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufMOOC backbone using Netty and Protobuf
MOOC backbone using Netty and Protobuf
 
Concurrency in c#
Concurrency in c#Concurrency in c#
Concurrency in c#
 
.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7.NET Core, ASP.NET Core Course, Session 7
.NET Core, ASP.NET Core Course, Session 7
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routing
 
.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF Core
 
Android AsyncTask Tutorial
Android AsyncTask TutorialAndroid AsyncTask Tutorial
Android AsyncTask Tutorial
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
 
.Net Threading
.Net Threading.Net Threading
.Net Threading
 
DATA BASE MANAGEMENT SYSTEM - SHORT NOTES
DATA BASE MANAGEMENT SYSTEM - SHORT NOTESDATA BASE MANAGEMENT SYSTEM - SHORT NOTES
DATA BASE MANAGEMENT SYSTEM - SHORT NOTES
 
25250716 seminar-on-ajax text
25250716 seminar-on-ajax text25250716 seminar-on-ajax text
25250716 seminar-on-ajax text
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.Servlet
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Command pattern in java
Command pattern in javaCommand pattern in java
Command pattern in java
 

Similar to Concurrency and parallel in .net

Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresHitendra Kumar
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10Minal Maniar
 
Eventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionEventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionMarc Seeger
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in androidRakesh Jha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
Cloud Module 3 .pptx
Cloud Module 3 .pptxCloud Module 3 .pptx
Cloud Module 3 .pptxssuser41d319
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in RubyPolcode
 
MC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionMC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionNarinder Kumar
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESsuthi
 
Parallel programs to multi-processor computers!
Parallel programs to multi-processor computers!Parallel programs to multi-processor computers!
Parallel programs to multi-processor computers!PVS-Studio
 
5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptxMohamedBilal73
 
Dot net interview questions and asnwers
Dot net interview questions and asnwersDot net interview questions and asnwers
Dot net interview questions and asnwerskavinilavuG
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrencyaviade
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 

Similar to Concurrency and parallel in .net (20)

Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Eventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionEventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introduction
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Cloud Module 3 .pptx
Cloud Module 3 .pptxCloud Module 3 .pptx
Cloud Module 3 .pptx
 
1844 1849
1844 18491844 1849
1844 1849
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in Ruby
 
MC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionMC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall session
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
 
Parallel programs to multi-processor computers!
Parallel programs to multi-processor computers!Parallel programs to multi-processor computers!
Parallel programs to multi-processor computers!
 
5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx
 
Dot net interview questions and asnwers
Dot net interview questions and asnwersDot net interview questions and asnwers
Dot net interview questions and asnwers
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Multithreading
MultithreadingMultithreading
Multithreading
 

Recently uploaded

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Concurrency and parallel in .net

  • 2. Speaking of concurrency, we have to start talking about threads. Ironically, the reason behind implementing threads was to isolate programs from each other. Back in the early days of Windows, versions 3.* used cooperative multitasking. This meant that the operating system executed all the programs on a single execution loop, and if one of those programs hung, every other program and the operating system itself would stop responding as well and then it would be required to reboot the machine to resolve this problem. Why Concurrency
  • 3. Simply using multiple threads in a program is not a very complicated task. If your program can be easily separated into several independent tasks, then you just run them in different threads However, usually real world programs require some interaction between these threads, such as exchanging information to coordinate their work Dealing with this shared state is the root of almost every problem related to parallel programming What’s the problem?
  • 4. Using locks How Solve Problem It is important to always release a lock after acquiring it. Always put the code for releasing a lock into the finally block of the try / catch statement, otherwise any exception thrown before releasing this lock would leave the ReaderWriterLock object in a locked state, preventing any further access to this lock.
  • 5. Lock localization The first thing to take into account when writing parallel code is to lock as little code as possible, and ensure that the code inside the lock runs as fast as possible This makes it less deadlock-prone and scale better with the number of CPU cores Optimization strategy Shared data minimization It is a common situation when we lock over the whole collection every time we write into it, instead of thinking and lowering the amount of locks and the data being locked. Organizing concurrent access and data storage in a way that it minimizes the number of locks can lead to a significant performance increase
  • 6. Parallel processing Doing lots of work by dividing it up among multiple threads that run concurrently Parallel processing is one type of multithreading, and multithreading is one type of concurrency
  • 7. Asynchronous programming A form of concurrency that uses futures or callbacks to avoid unnecessary threads the thread that starts the operation is free to do When the operation completes, it notifies its future or invokes its callback or event to let the application know the operation is finished.
  • 8. Introduction to Asynchronous Programming Asynchronous programming has two primary benefits • asynchronous programming enables responsiveness. can remain responsive to user input while it’s working • asynchronous server application can usually scale an order of magnitude better than sync Both benefits of asynchronous programming derive from the same underlying aspect: frees up a thread
  • 9. One of the most essential tasks when writing parallel code is to divide your program into subsets that will run in parallel and communicate between each other. Sometimes the task naturally divides into separate pieces, but usually it is up to you to choose which parts to make parallel. Should we use a small number of large tasks, many small tasks, or maybe large and small tasks at the same time? Understanding Parallelism Granularity
  • 10. what is a thread’s cost for the operating system and CPU? What number of threads is optimal? In Windows and in the 32-bit mode, the maximum number of threads in your process is restricted by the virtual address space available, which is two gigabytes. A thread stack’s size is one megabyte, so we can have maximum 2,048 threads. In a 64-bit OS for a 32-bit process, it should be 4,096 The number of threads Please be aware that if we run this in 64-bit mode, the program will exhaust system resources and might cause the OS to hang
  • 11. The first reason is that when we run a 32-bit process on 64-bit Windows, a thread will have a 64-bit stack as well, and the actual stack allocation will be 1 MB + 256 KB of the 64-bit stack. The second reason is that our process is limited to 2 GB of the address space. If we want to use more, we have to specify a special flag, IMAGE_FILE_LARGE_ADDRESS_AWARE, for our program, which is set using the /LARGEADDRESSAWARE linker option. We cannot set this flag directly in Visual Studio, but we are able to use a tool called EditBin.exe, which is included in Visual Studio installation. we getting 1,522 threads while we expected to get about 4,000 when we compiled our program in 32-bit mode?
  • 12.
  • 13. Thread Pool creating a thread is quite an expensive operation. In addition to this, creating more and more threads is not efficient. To make asynchronous operations easier, in Common Language Runtime there is a thread pool There are two types of threads inside the thread pool: o worker threads o I/O threads There is one thread pool per process.
  • 15. This chart shows that when we reduce task size, we increase performance until some point. Then the task size becomes small enough to achieve full CPU workload. Making tasks smaller becomes ineffective due to an overall task overhead increase.
  • 16. if we need multiple threads to add some item to a collection, we cannot just call the Add method of a shared instance of the List<T> type. It will lead to unpredictable results, and most probably the program will end up throwing a weird exception Using Concurrent Data Structures
  • 17. thread contention it can significantly decrease your program performance Concurrent collections This approach is called coarse-grained locking ,A complicated, but an efficient, approach is to use fine-grained locking, so we can provide an exclusive access only to the parts of the collection that are in use. For example, if the underlying data storage is an array, we can create multiple locks that will cover the corresponding array parts. This approach requires determining the required lock first, but it will also allow a non-blocking access to the different parts of the array. This will use locks only when there is a concurrent access to the same data. In certain scenarios, the performance difference will be huge
  • 18. ConcurrentDictionary Implementation details The Tables class contains the following most important fields: m_buckets: This is an array of buckets; each of the buckets contains a singly-linked list of nodes with dictionary data. m_locks: This is an array of locks; each lock provides synchronized access to one or more buckets. m_countPerLock: This is an array of counters; each counter contains a total number of nodes that are protected by the corresponding lock. For example, if we look at the previous scheme, where the first lock protects the first two buckets, the m_countPerLock[0] element will contain the value of 5. m_comparer: This is an IEqualityComparer<TKey> object that contains the logic for calculating the hash value of a key object. The entire ConcurrentDictionary state is placed in a separate Tables class instance in the m_tables field. This makes it possible to have an atomic state change operation for the dictionary with the help of the compare-and-swap (CAS) operations.
  • 20. Each worker thread starts running our code and waits two seconds doing nothing. Then, they return the response. As we may recall from the previous chapters, thread pool worker threads are a limited resource, and when we start issuing 1,000 concurrent requests in a short time, all the worker threads become occupied running Thread.Sleep. At the same time Task.Delay uses a timer object under the hood. This allows an ASP.NET worker thread to start a wait operation, and then to return to the application pool and process some other requests. When two seconds pass, the timer posts a continuation callback to an available ASP.NET thread pool worker thread. This allows the application to process more user requests, since worker threads are not blocked. So, this timer object helps our application to remain fast and scalable.
  • 21. I/O and CPU-bound tasks there are tasks related to input/output processes, such as reading or writing a file, issuing a network request, or even performing a query against a database. These operations usually take much more time compared to CPU bound work. So does this mean that our worker threads will be locked for a longer time and the application will fail to scale? When we mention a file or network request, we know that there are physical devices such as disks and network cards that actually execute these operations. These devices have controllers, and a controller in this context means a micro- computer with its own CPU. To perform an I/O bound task, we do not need to waste the main CPU’s time, it is enough to give all the required data to the I/O device controller, and it will perform the I/O operation and return the results with the help of a device driver. To communicate with the I/O devices, Windows uses a special object called I/O Completion Port (or IOCP). It behaves pretty much like a timer, but the signals are coming from the I/O devices and not from the internal clock. This means that, while an I/O operation is in progress, we can reuse the ASP.NET worker thread to serve other requests, and thus achieve good scalability.
  • 22. Notice a new entity called the I/O thread in the preceding diagram. There is a separate smaller pool of I/O threads inside this .NET thread pool. The I/O threads are not different from the usual worker threads, but they are being used only to execute continuation callbacks for asynchronous I/O operations. If we use general worker threads for this purpose, it can happen that there are no worker threads available and we cannot complete the I/O operation, which in turn will lead to deadlocks. Using a separate thread pool will help to prevent this, but we also need to be very careful not to cause I/O threads starvation
  • 23. Real and fake asynchronous I/O operations only the numbers two and three writes are asynchronous. if we do not specify the correct options for the file API we use, the file will provide us with the wrong kind of asynchrony that uses worker threads for the I/O process and thus is not scalable
  • 24. This workflow is even worse than the usual synchronous code, because there is an additional performance overhead related to running this part of the operation on a different worker thread. We end up wasting worker thread for the entire time of the I/O operation anyway, and this is fake asynchronous I/O. It is actually a CPU-bound operation that will affect the scalability and performance of your application.
  • 25. Technically, the most efficient way will be to run such work synchronously and scale horizontally by adding more and more servers to be able to handle increasing load. Nevertheless, it can happen that this CPU-bound work is not the only responsibility of a server application. if there is a long running operation, a web application registers it into some data store, returns a unique identifier of this operation to the client, and posts this operation to a special queue. Then there is a separate pool of worker processes that monitor this queue, get tasks from them, process them, and write results to a data store. When the client arrives next time, the web application checks whether the task has been already completed by any worker and if it has, the application returns the result to the client. CPU-bound tasks and queues
  • 26. While a server application in general has to be scalable before everything else, a client application is different. It is usually intended to run for one user on one computer, and thus the user expects it to run fast and not cause troubles for the other applications running in the system. While the second part is usually handled by the operating system, the application’s performance in getting things done and reacting to user input is crucial for a positive user experience. we have to learn how the UI works and the UI threading architecture. UI threads and message loops Concurrency in the User Interface
  • 27. Troubleshooting Parallel Programs A concurrent program like any usual program can contain programming errors that could lead to incorrect results. However, concurrency usually leads programs to become more complicated, causing errors to be trickier and harder to find. There are typical problems related to concurrent shared state access conditions and deadlocks, but there are many other kinds of problems specific to concurrent programs.
  • 28. This is one more problem type, not strictly related to concurrent programming, but much more common with it. In computer programming jargon, a heisenbug is a software bug that seems to disappear or alter its behaviour when one attempts to study it. The term is a pun on the name of Werner Heisenberg, the physicist who first asserted the observer effect of quantum mechanics, which states that the act of observing a system inevitably alters its state. These problems are usually extremely hard to reproduce and debug, since they usually appear in some special conditions such as high user load, or some specific events timing, and more. This is the kind of bug which you will inevitably meet while developing concurrent applications. Heisenbugs
  • 29. Writing tests: This is a very important step that can dramatically reduce bugs in your code. With these tests, it is possible to detect problems right after writing the code, or after deploying your application into a test environment. Debugging: Visual Studio has specific features and tools to make debugging concurrent applications easier. Performance measurement and profiling: This is one more very important step that can help to detect whether your program spends too much time switching between threads or blocking them instead of doing its job.
  • 30. Test Async Method Mocking asynchronous dependencies can be a bit awkward at first. It’s a good idea to at least test how your methods respond to synchronous, synchronous errors, and asynchronous success
  • 31. When testing asynchronous code, deadlocks and race conditions may surface more often than when testing synchronous code Do not forget to await the task returned by ThrowsAsync, If you forget the await and ignore the compiler warning, your unit test will always silently succeed regardless of your method’s behavior -------------------------------------------------------------------------------------------------------- It’s better to test for an exception thrown at a specific point rather than testing for an exception at any time during the test. Instead of ExpectedException, use ThrowsAsync
  • 32. Async Void Avoid async void! It is possible to have an async method return void, but you should only do this if you’re writing an async event handler. A regular async method without a return value should return Task, not void.
  • 33. parallelism • Data parallelism is when you have a bunch of data items to process, and the processing of each piece of data is mostly independent from the other pieces • Task parallelism is when you have a pool of work to do, and each piece of work is mostly independent from the other pieces data parallelism Parallel.ForEach AsParallel task parallelism Parallel.Invoke data parallelism and task parallelism are similar, “processing data” is a kind of “work” Many parallelism problems can be solved either way
  • 34.
  • 35. Reactive Programming (Rx) Reactive programming has a higher learning curve than other forms of concurrency, and the code can be harder to maintain unless you keep up with your reactive skills Reactive programming enables you to treat a stream of events like a stream of data. Reactive programming is based on the notion of observable streams When you subscribe to an observable stream, you’ll receive any number of data items (OnNext), and then the stream may end with a single error (OnError) or “end of stream” notification (OnCompleted)
  • 36. The example code starts with a counter running off a periodic timer (Interval) and adds a timestamp to each event (Timestamp). It then filters the events to only include even counter values (Where), selects the timestamp values (Timestamp), and then as each resulting timestamp value arrives, writes it to the debugger (Subscribe) More read : TPL Dataflows
  • 37. Introduction to Multithreaded Programming There is almost no need for you to ever create a new thread yourself. The only time you should ever create a Thread instance is if you need an STA thread for COM interop. A thread is a low-level abstraction.
  • 38. you need to aggregate the results. Examples of aggregation are summing up values or finding their average. Parallel Aggregation