SlideShare a Scribd company logo
1 of 31
eleks.com
Multithreading/Multitasking.
Task Parallel Library.
Patterns.
by Oleksandr Kravchuk, JR .NET Developer
What is the Multithreading?
An ability that allows you to run several
sections of code simultaneously.
Or pretend like. //For 1 CPU
core
Process
Executing instance of a
program. Virtual memory and
no direct communication.
Threads container
Thread
Basic unit to which the operating
system allocates processor time.
Executes within the context of a
process and shares the same
resources allotted to the process by
the kernel.
On an Operating System level
Insides:
• PID
• Memory (Code and Data, Stack, Heap,
Shared Memory…)
• File Descriptors
• Registers
• Kernel State (Process State, Priority,
Statistics)
Insides:
• Thread Kernel Object
• Thread Environment Block (TEB)
• Stacks (User-mode and Kernel-mode)
Thread in numbers
• Kernel State (Kernel Object)
- 700 bytes for x86
- 1240 bytes for x64
• Thread environment block
- 1 memory page (4 Kb)
• User-mode stack
- 1+ Mb
• Kernel-mode stack
- 12 Kb for x86
- 24 Kb for x64
Note: Also, whenever a thread is created in a process, all
unmanaged DLLs loaded in that process have their
DllMain method called. Similarly, whenever a thread
dies.
Hardware trends
CPU development:
● Single-core
● Multi-socket motherboards
● Single-core with Hyper-threading
● Multi-core
● Multi-core with Hyper-threading
Context switches
• Kernel-level scheduler responsibility
• Schedule applies to threads, not to processes
• Relies on the priority (process priority + thread priority)
Process and Thread Priority
Relations
Relative Thread
Priority
Process priority Class
Idle Below Normal Normal Above Normal High Real-time
Time-Critical 15 15 15 15 15 31
Highest 6 8 10 10 12 26
Above Normal 5 7 9 9 11 25
Normal 4 6 8 8 10 24
Below Normal 3 5 7 7 9 23
Lowest 2 4 6 6 8 22
Idle 1 1 1 1 1 16
Where should I use
Threads?
• Client-side GUI applications where
responsiveness is important.
• Client-side and server-side
applications where non-sequentially
execution is possible. For
performance improvements
Thread usage example.
Briefly about Thread class
• Return type is void
• Constructors:
- Thread(ThreadStart)
- Thread(ParameterizedThreadStart)
- Thread(ThreadStart, Int32)
- Thread(ParameterizedThreadStart, Int32)
, where ThreadStart and ParameterizedThreadStart
are the delegates, lambdas, closures, Action<T>,
Func<T>, etc. Also, you may limit thread stack size
By passing second parameter.
• Start() method to run the thread
• Use IsAlive property to wait for the thread start
• Join() method to wait till thread ends
• Use closures to simplify value return
• Set thread IsBackground property to true for
immediately suspension when parent foreground
thread ends
• Exceptions can be caught only on the same
thread
Producer/Consumer Pattern
• BlockingCollection<T> as queue
• Variable number of producer/consumer threads
P/C Pattern implementation
public class Producer : IDisposable {
private volatile bool _isRunning;
private Thread _commandGetThread;
private object _commandGetterLocker = new object();
private int _sleepInterval;
private Consumer _executor;
public Producer(Consumer executor, int sleepInterval) {
… //Set defaults
_isRunning = true;
_commandGetThread = new Thread(CommandRequestSend);
_commandGetThread.Start();
}
private void CommandRequestSend() {
while (_isRunning) {
lock (_commandGetterLocker) {
… //GetCommands code goes here
_executor.EnqueueCommands(webCommands);
}
Thread.Sleep(_sleepInterval);
}
}
public void Dispose() { … } //use Join() instead of Abort()
}
public class Consumer : IDisposable {
private volatile bool _isRunning;
private object locker = new object();
private Thread[] executants;
private ICommandRepository _commandsRepo = new CommandListRepository();
public Consumer(int executorsCount) {
_isRunning = true;
executants = new Thread[executorsCount];
for (int i = 0; i < executorsCount; i++)
(executants[i] = new Thread(Execute)).Start();
}
public void EnqueueTask(List<BLCommand> commands) {
lock (locker) {
_commandsRepo.AddCommands(commands);
Monitor.PulseAll(locker);
}
}
void Execute() {
while (_isRunning) {
lock (locker) {
while (_commandsRepo.IsEmpty()) Monitor.Wait(locker);
commandClient = _commandsRepo.GetCommand();
}
if (commandClient == null) return;
… //Execute Command Code (better wrap with try-catch)
}
}
public void Dispose() { … } //enque null in each thread and join
}
CLR ThreadPool
• Class ThreadPool was introduced in
.Net Framework 3.5. Later, Task
approach will use it in 4.0 version
• ThreadPool works on CLR level. It
has highly intelligent algorithm for
thread management.
• Only busy threads in pool
• To perform asynchronous operation:
just call
ThreadPool.QueueUserWorkItem()
• What is ideal thread number?
• How queues are scheduled?
• What is Work-Stealing?
• How CLR manages thread
number?
How the Thread Pool Manages Its
Threads?
Thread Pool usage example.
Tasks concept
• Return value from asynchronous operation. Just call task.Result
• You know, when operation completes
• Task class for void and Task<T> generic for T object return
• No-headache with exception handling. Throws AggregateException with inner
exceptions tree that corresponds to Tasks tree
• Task start does not guarantee execution in separate thread!
ThreadPool.QueueUserWorkItem(SomeLongTermFunction);
var task = new Task(SomeLongTermFunction);
task.Start();
Tasks states
*Also, task can be in waiting (for activation, to run, for children’s completion) states
Waiting
• task.Wait() instead of while(!task.IsCompleted)
• Task.WaitAny() for response processing with best performance
• Task.WaitAll() if you need all results
Cancelling
1. Create CancellationTokenSource object and pass its Token property to task constructor
2. Start task and call Cancel() method on CancellationTokenSource object
3. Task will stop and throw AggregateException
Continuations
In order to write scalable software, you must not have your threads
block.
Calling task.Wait() will pause current thread until Result property
became available
Its better for performance to start next task immediately after previous.
For this case, there are .ContinueWith() extension for task.
Usage sample: var task = new Task(SomeLongTermFunction, cancelToken.Token);
task.ContinueWith(parentTask => AnotherLongTermFunction(),
TaskContinuationOptions.NotOnFaulted);
task.Start();
Factories
To create a bunch of tasks that
return void, then you will
construct a TaskFactory.
If you want to create a bunch of
tasks that have a specific return
type, then you will construct a
TaskFactory<TResult>
Schedulers
TaskScheduler object is responsible
for executing scheduled tasks and
also exposes task information
to the Visual Studio debugger
The FCL ships with two
TaskScheduler-derived types:
• the thread pool task scheduler
• synchronization context task
scheduler.
By default, all applications use the
thread pool task scheduler.
Tasks are very flexible
var factory =
new TaskFactory<int>(TaskScheduler
.FromCurrentSynchronizationContext());
factory.StartNew(() => GetFibonacciNumber(1));
factory.StartNew(() => GetFibonacciNumber(2));
factory.StartNew(() => GetFibonacciNumber(3));
To simplify writing code for parallel execution, there are:
The Parallel class
Parallel.For(fromInclusive, toConclusive, index => method(index));
Parallel.ForEach(IEnumerable, item => method(item));
Parallel.Invoke(method0(), method1(), method2()…);
They all have overloaded versions that takes ParallelOption object as parameter.
ParallelOption contains such settings:
- MaxThreadNumbers
- CancellationToken
- TaskScheduler
Also, there is possibilty in TPL that allows interaction among parallel parts
of algorythm.
Use localInit and localFinal parameters.
Code sample:
Tasks interaction in Parallel
var files = Directory.EnumerateFiles(path, searchPattern, searchOption);
var masterTotal = 0;
var result = Parallel.ForEach<String, int>(
files,
() => { return 0; /* Set taskLocalTotal initial value to 0*/ },
(file, loopState, index, taskLocalTotal) =>
{
// body: Invoked once per work item
// Get this file's size and add it to this task's running total
var fileLength = 0;
FileStream fs = null;
try
{
fs = File.OpenRead(file);
fileLength = (int) fs.Length;
}
catch (IOException) { /* Ignore any files we can't access */ }
finally
{
if (fs != null) fs.Dispose();
}
return taskLocalTotal + fileLength;
},
taskLocalTotal =>
{
// localFinally: Invoked once per task at end
// Atomically add this task's total to the "master" total
Interlocked.Add(ref masterTotal, taskLocalTotal);
});
Console.WriteLine(masterTotal);
Not every algorithm could be
parallel
PLINQ
• Parallel Language Integrated Query – set of extensions that
allows parallel processing of ParallelQuery<T> collection.
• To transform IEnumerable<T> into ParallelQuery<T> - just call
AsParallel() on it (AsSequential() for vice versa)
• Supports almost the same functionality, as the ordinar LINQ.
• Also, offers some additional ParallelEnumerable methods that
you can call to control how the query is processed:
- WithCancellation(CancellationToken)
- WithDegreeOfParalelism(Int32)
- WithExecutionMode(ParallelExecutionMode)
- WithMergeOptions(ParallelMergeOption)
Parallel and PLINQ usage example.
Allows you to perform a
Periodic Compute-Bound
Operation.
Timers System.Threading.Timer usage
example
To many timers in .Net:
1. System.Threading.Timer
2. System.Windows.Forms.Timer
3. System.Windows.Threading.DispatcherTimer
(Silverlight and WPF)
4. Windows.UI.Xaml’s DispatcherTimer (Windows
Store Apps)
5. System.Timers.Timer. Obsolete class. Wrapper for
System.Threading.Timer.
private static Timer s_timer;
public static void Main()
{
Console.WriteLine("Checking status every 2 seconds");
// Create the Timer ensuring that it never fires. This ensures
// that s_timer refers to it BEFORE Status is invoked by a
// thread pool thread
s_timer = new Timer(Status, null, Timeout.Infinite,
Timeout.Infinite);
// Now that s_timer is assigned to, we can let the timer fire
// knowing that calling Change in Status will not throw a
// NullReferenceException
s_timer.Change(0, Timeout.Infinite);
Console.ReadLine(); // Prevent the process from terminating
}
// This method's signature must match the TimerCallback delegate
private static void Status(Object state)
{
// This method is executed by a thread pool thread
Console.WriteLine("In Status at {0}", DateTime.Now);
Thread.Sleep(1000); // Simulates other work (1 second)
// Just before returning, have the Timer fire again in 2
seconds
s_timer.Change(2000, Timeout.Infinite);
// When this method returns, the thread goes back
// to the pool and waits for another work item
}
• Object should have GetAwaiter()
method implemented to be available
for await
• Async method without awaits inside
will be executed synchronously
• Compiler will create continuations for
code after await
• There are a lot of async functions in
FCL that can be easily found by
suffix “Async”
Async/Await
• Exception can be catched from main
thread only if async method is
awaited
• Using await with a Task, the first inner
exception is thrown instead of an
AggregateException
• “await” keyword inside of a catch{}
and a finally {} blocks are supported
from C# 6.0
Async/Await example.
• Asynchronous Programming Model (APM)
• Event-based Asynchronous Pattern (EAP)
• Task-based Asynchronous Pattern (TAP)
Asynchronous Programming
Patterns
APM to TAP conversion:
await Task.Factory.FromAsync(
stream.BeginRead, stream.EndRead, null);
Inspired by Technology.
Driven by Value.
Find us at eleks.com
Have a question? Write to eleksinfo@eleks.com

More Related Content

What's hot

Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#Rizwan Ali
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javajunnubabu
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyducquoc_vn
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamNavaneethan Naveen
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in JavaM. Raihan
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkArun Mehra
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in javaAbhra Basak
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrencykshanth2101
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 

What's hot (20)

Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 

Similar to .NET Multithreading/Multitasking

Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLArie Leeuwesteijn
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaRuben Inoto Soto
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksWO Community
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Aravindharamanan S
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongPROIDEA
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join frameworkMinh Tran
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and ParallelizationDmitri Nesteruk
 
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...Gerke Max Preussner
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...Gerke Max Preussner
 

Similar to .NET Multithreading/Multitasking (20)

Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
 
Tech talk
Tech talkTech talk
Tech talk
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
concurrency
concurrencyconcurrency
concurrency
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background Tasks
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
Thread 1
Thread 1Thread 1
Thread 1
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and Parallelization
 
Threads
ThreadsThreads
Threads
 
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
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
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
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
 
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.
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
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
 
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
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
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
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
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
 
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)
 
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 ...
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
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...
 
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
 
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
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 

.NET Multithreading/Multitasking

  • 2. What is the Multithreading? An ability that allows you to run several sections of code simultaneously. Or pretend like. //For 1 CPU core
  • 3.
  • 4. Process Executing instance of a program. Virtual memory and no direct communication. Threads container Thread Basic unit to which the operating system allocates processor time. Executes within the context of a process and shares the same resources allotted to the process by the kernel. On an Operating System level Insides: • PID • Memory (Code and Data, Stack, Heap, Shared Memory…) • File Descriptors • Registers • Kernel State (Process State, Priority, Statistics) Insides: • Thread Kernel Object • Thread Environment Block (TEB) • Stacks (User-mode and Kernel-mode)
  • 5. Thread in numbers • Kernel State (Kernel Object) - 700 bytes for x86 - 1240 bytes for x64 • Thread environment block - 1 memory page (4 Kb) • User-mode stack - 1+ Mb • Kernel-mode stack - 12 Kb for x86 - 24 Kb for x64 Note: Also, whenever a thread is created in a process, all unmanaged DLLs loaded in that process have their DllMain method called. Similarly, whenever a thread dies.
  • 6. Hardware trends CPU development: ● Single-core ● Multi-socket motherboards ● Single-core with Hyper-threading ● Multi-core ● Multi-core with Hyper-threading
  • 7. Context switches • Kernel-level scheduler responsibility • Schedule applies to threads, not to processes • Relies on the priority (process priority + thread priority)
  • 8. Process and Thread Priority Relations Relative Thread Priority Process priority Class Idle Below Normal Normal Above Normal High Real-time Time-Critical 15 15 15 15 15 31 Highest 6 8 10 10 12 26 Above Normal 5 7 9 9 11 25 Normal 4 6 8 8 10 24 Below Normal 3 5 7 7 9 23 Lowest 2 4 6 6 8 22 Idle 1 1 1 1 1 16
  • 9. Where should I use Threads? • Client-side GUI applications where responsiveness is important. • Client-side and server-side applications where non-sequentially execution is possible. For performance improvements
  • 11. Briefly about Thread class • Return type is void • Constructors: - Thread(ThreadStart) - Thread(ParameterizedThreadStart) - Thread(ThreadStart, Int32) - Thread(ParameterizedThreadStart, Int32) , where ThreadStart and ParameterizedThreadStart are the delegates, lambdas, closures, Action<T>, Func<T>, etc. Also, you may limit thread stack size By passing second parameter. • Start() method to run the thread • Use IsAlive property to wait for the thread start • Join() method to wait till thread ends • Use closures to simplify value return • Set thread IsBackground property to true for immediately suspension when parent foreground thread ends • Exceptions can be caught only on the same thread
  • 12. Producer/Consumer Pattern • BlockingCollection<T> as queue • Variable number of producer/consumer threads
  • 13. P/C Pattern implementation public class Producer : IDisposable { private volatile bool _isRunning; private Thread _commandGetThread; private object _commandGetterLocker = new object(); private int _sleepInterval; private Consumer _executor; public Producer(Consumer executor, int sleepInterval) { … //Set defaults _isRunning = true; _commandGetThread = new Thread(CommandRequestSend); _commandGetThread.Start(); } private void CommandRequestSend() { while (_isRunning) { lock (_commandGetterLocker) { … //GetCommands code goes here _executor.EnqueueCommands(webCommands); } Thread.Sleep(_sleepInterval); } } public void Dispose() { … } //use Join() instead of Abort() } public class Consumer : IDisposable { private volatile bool _isRunning; private object locker = new object(); private Thread[] executants; private ICommandRepository _commandsRepo = new CommandListRepository(); public Consumer(int executorsCount) { _isRunning = true; executants = new Thread[executorsCount]; for (int i = 0; i < executorsCount; i++) (executants[i] = new Thread(Execute)).Start(); } public void EnqueueTask(List<BLCommand> commands) { lock (locker) { _commandsRepo.AddCommands(commands); Monitor.PulseAll(locker); } } void Execute() { while (_isRunning) { lock (locker) { while (_commandsRepo.IsEmpty()) Monitor.Wait(locker); commandClient = _commandsRepo.GetCommand(); } if (commandClient == null) return; … //Execute Command Code (better wrap with try-catch) } } public void Dispose() { … } //enque null in each thread and join }
  • 14. CLR ThreadPool • Class ThreadPool was introduced in .Net Framework 3.5. Later, Task approach will use it in 4.0 version • ThreadPool works on CLR level. It has highly intelligent algorithm for thread management. • Only busy threads in pool • To perform asynchronous operation: just call ThreadPool.QueueUserWorkItem()
  • 15. • What is ideal thread number? • How queues are scheduled? • What is Work-Stealing? • How CLR manages thread number? How the Thread Pool Manages Its Threads?
  • 16. Thread Pool usage example.
  • 17. Tasks concept • Return value from asynchronous operation. Just call task.Result • You know, when operation completes • Task class for void and Task<T> generic for T object return • No-headache with exception handling. Throws AggregateException with inner exceptions tree that corresponds to Tasks tree • Task start does not guarantee execution in separate thread! ThreadPool.QueueUserWorkItem(SomeLongTermFunction); var task = new Task(SomeLongTermFunction); task.Start();
  • 18. Tasks states *Also, task can be in waiting (for activation, to run, for children’s completion) states
  • 19. Waiting • task.Wait() instead of while(!task.IsCompleted) • Task.WaitAny() for response processing with best performance • Task.WaitAll() if you need all results Cancelling 1. Create CancellationTokenSource object and pass its Token property to task constructor 2. Start task and call Cancel() method on CancellationTokenSource object 3. Task will stop and throw AggregateException
  • 20. Continuations In order to write scalable software, you must not have your threads block. Calling task.Wait() will pause current thread until Result property became available Its better for performance to start next task immediately after previous. For this case, there are .ContinueWith() extension for task. Usage sample: var task = new Task(SomeLongTermFunction, cancelToken.Token); task.ContinueWith(parentTask => AnotherLongTermFunction(), TaskContinuationOptions.NotOnFaulted); task.Start();
  • 21. Factories To create a bunch of tasks that return void, then you will construct a TaskFactory. If you want to create a bunch of tasks that have a specific return type, then you will construct a TaskFactory<TResult> Schedulers TaskScheduler object is responsible for executing scheduled tasks and also exposes task information to the Visual Studio debugger The FCL ships with two TaskScheduler-derived types: • the thread pool task scheduler • synchronization context task scheduler. By default, all applications use the thread pool task scheduler. Tasks are very flexible var factory = new TaskFactory<int>(TaskScheduler .FromCurrentSynchronizationContext()); factory.StartNew(() => GetFibonacciNumber(1)); factory.StartNew(() => GetFibonacciNumber(2)); factory.StartNew(() => GetFibonacciNumber(3));
  • 22. To simplify writing code for parallel execution, there are: The Parallel class Parallel.For(fromInclusive, toConclusive, index => method(index)); Parallel.ForEach(IEnumerable, item => method(item)); Parallel.Invoke(method0(), method1(), method2()…); They all have overloaded versions that takes ParallelOption object as parameter. ParallelOption contains such settings: - MaxThreadNumbers - CancellationToken - TaskScheduler
  • 23. Also, there is possibilty in TPL that allows interaction among parallel parts of algorythm. Use localInit and localFinal parameters. Code sample: Tasks interaction in Parallel var files = Directory.EnumerateFiles(path, searchPattern, searchOption); var masterTotal = 0; var result = Parallel.ForEach<String, int>( files, () => { return 0; /* Set taskLocalTotal initial value to 0*/ }, (file, loopState, index, taskLocalTotal) => { // body: Invoked once per work item // Get this file's size and add it to this task's running total var fileLength = 0; FileStream fs = null; try { fs = File.OpenRead(file); fileLength = (int) fs.Length; } catch (IOException) { /* Ignore any files we can't access */ } finally { if (fs != null) fs.Dispose(); } return taskLocalTotal + fileLength; }, taskLocalTotal => { // localFinally: Invoked once per task at end // Atomically add this task's total to the "master" total Interlocked.Add(ref masterTotal, taskLocalTotal); }); Console.WriteLine(masterTotal);
  • 24. Not every algorithm could be parallel
  • 25. PLINQ • Parallel Language Integrated Query – set of extensions that allows parallel processing of ParallelQuery<T> collection. • To transform IEnumerable<T> into ParallelQuery<T> - just call AsParallel() on it (AsSequential() for vice versa) • Supports almost the same functionality, as the ordinar LINQ. • Also, offers some additional ParallelEnumerable methods that you can call to control how the query is processed: - WithCancellation(CancellationToken) - WithDegreeOfParalelism(Int32) - WithExecutionMode(ParallelExecutionMode) - WithMergeOptions(ParallelMergeOption)
  • 26. Parallel and PLINQ usage example.
  • 27. Allows you to perform a Periodic Compute-Bound Operation. Timers System.Threading.Timer usage example To many timers in .Net: 1. System.Threading.Timer 2. System.Windows.Forms.Timer 3. System.Windows.Threading.DispatcherTimer (Silverlight and WPF) 4. Windows.UI.Xaml’s DispatcherTimer (Windows Store Apps) 5. System.Timers.Timer. Obsolete class. Wrapper for System.Threading.Timer. private static Timer s_timer; public static void Main() { Console.WriteLine("Checking status every 2 seconds"); // Create the Timer ensuring that it never fires. This ensures // that s_timer refers to it BEFORE Status is invoked by a // thread pool thread s_timer = new Timer(Status, null, Timeout.Infinite, Timeout.Infinite); // Now that s_timer is assigned to, we can let the timer fire // knowing that calling Change in Status will not throw a // NullReferenceException s_timer.Change(0, Timeout.Infinite); Console.ReadLine(); // Prevent the process from terminating } // This method's signature must match the TimerCallback delegate private static void Status(Object state) { // This method is executed by a thread pool thread Console.WriteLine("In Status at {0}", DateTime.Now); Thread.Sleep(1000); // Simulates other work (1 second) // Just before returning, have the Timer fire again in 2 seconds s_timer.Change(2000, Timeout.Infinite); // When this method returns, the thread goes back // to the pool and waits for another work item }
  • 28. • Object should have GetAwaiter() method implemented to be available for await • Async method without awaits inside will be executed synchronously • Compiler will create continuations for code after await • There are a lot of async functions in FCL that can be easily found by suffix “Async” Async/Await • Exception can be catched from main thread only if async method is awaited • Using await with a Task, the first inner exception is thrown instead of an AggregateException • “await” keyword inside of a catch{} and a finally {} blocks are supported from C# 6.0
  • 30. • Asynchronous Programming Model (APM) • Event-based Asynchronous Pattern (EAP) • Task-based Asynchronous Pattern (TAP) Asynchronous Programming Patterns APM to TAP conversion: await Task.Factory.FromAsync( stream.BeginRead, stream.EndRead, null);
  • 31. Inspired by Technology. Driven by Value. Find us at eleks.com Have a question? Write to eleksinfo@eleks.com