SlideShare a Scribd company logo
http://www.slideshare.net/martenrange/
Mårten Rånge 
Ericsson AB 
@marten_range
Concurrency 
Examples for .NET
Responsive
Performance 
Scalable algorithms
Three pillars of Concurrency 
 Scalability (CPU) 
 Parallel.For 
 Responsiveness 
 Task 
 async/await 
 Consistency 
 lock 
 Interlocked.* 
 Mutex/Event/Semaphore 
 Monitor
Responsiveness
string ReadSomeText (string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = sr.ReadToEnd (); 
return result; 
} 
} 
// Blocks the thread until IO completes 
var text = ReadSomeText ("SomeText.txt");
Asyncronous programming allows 
programs to be responsive
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var first = await sr.ReadLineAsync(); 
var second = await sr.ReadLineAsync(); 
var third = await sr.ReadLineAsync(); 
return first + second + third; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
It depends
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
It depends
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
It depends
What’s going on?
Your new ”best” friends 
 SynchronizationContext 
 Console apps 
 Continues on ThreadPool thread 
 WindowsFormsSynchronizationContext 
 Used by WindowsForms apps 
 Continues on the ”UI” thread 
 DispatcherSynchronizationContext 
 Used by WPF and ASP.NET apps 
 Continues on the ”same” thread
SynchronizationContext
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
Yes
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
No
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
No
DispatcherSynchronizationContext 
& 
WindowsFormsSynchronizationContext
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
No
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
Yes
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
Yes
So…
SynchronizationContext 
 Is ”invisible” 
 Is a thread-global state 
 Impacts the behavior of your code significantly 
 As an application developer you can make assumptions 
 As a library developer you can’t make assumptions
ConfigureAwait
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
Yes
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
--m_readingFiles; 
return result; 
} 
}
No
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
TraceThreadId (); 
return result; 
} 
}
No
async/await tries to be what we want
async/await reminds me of…
What we need 
 Do one thing 
 Responsiveness 
 Predictable semantics 
 Continuation is executed by a thread-pool thread 
 Visibility 
 Thread-switching should be visible in code
F# async 
// Focuses on responsiveness 
let gameLoop = 
async { 
// Switching to new thread is explicit 
do! Async.SwitchToNewThread () 
while true do 
// let! is like await in C# 
let! messages = fromVisual.AsyncDequeue 1000 
for message in messages do 
processMessage message 
}
C# 
 yield 
 Special support for enumerators 
 LINQ 
 Special support for SQL-like syntax 
 async/await 
 Special support for asynchronous programming
F# 
 seq { for x in 0..10 -> i } 
 Enumerators implemented using Computation Expressions 
 query { for c in db.Customers do select c } 
 SQL-like syntax implemented using Computation Expressions 
 async { let! r=fromVisual.AsyncDequeue 1000 in r } 
 Asynchronous programming using Computation Expressions
Computation Expressions
With async/await always consider the…
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext
Mårten Rånge 
Ericsson AB 
@marten_range
Links 
 Presentation 
 http://www.slideshare.net/martenrange/ 
 Code 
 https://github.com/mrange/presentations/ 
 DIY asynchronous workflows in F# 
 http://mrange.wordpress.com/2014/06/12/diy-asynchronous-workflows- 
in-f/ 
 Asynchronous programming with async/await 
 http://msdn.microsoft.com/en-us/library/hh191443.aspx

More Related Content

What's hot

Network programming1
Network programming1Network programming1
Network programming1
Soham Sengupta
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
sweetysweety8
 
Tcpsockets
TcpsocketsTcpsockets
Tcpsockets
mcjayaprasanna8
 
Asynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with NettyAsynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with Netty
Ersin Er
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
smile790243
 
AMC Minor Technical Issues
AMC Minor Technical IssuesAMC Minor Technical Issues
AMC Minor Technical Issues
Apache Traffic Server
 

What's hot (6)

Network programming1
Network programming1Network programming1
Network programming1
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Tcpsockets
TcpsocketsTcpsockets
Tcpsockets
 
Asynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with NettyAsynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with Netty
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
 
AMC Minor Technical Issues
AMC Minor Technical IssuesAMC Minor Technical Issues
AMC Minor Technical Issues
 

Similar to Concurrency - responsiveness in .NET

Asynchronous in dot net4
Asynchronous in dot net4Asynchronous in dot net4
Asynchronous in dot net4
Wei Sun
 
Web program-peformance-optimization
Web program-peformance-optimizationWeb program-peformance-optimization
Web program-peformance-optimization
xiaojueqq12345
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
apidays
 
15. text files
15. text files15. text files
15. text files
Konstantin Potemichev
 
[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline
Yusuke Kita
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
Nir Noy
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
Binu Bhasuran
 
Why async matters
Why async mattersWhy async matters
Why async matters
timbc
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
Oleg Podsechin
 
Async Programming in C# 5
Async Programming in C# 5Async Programming in C# 5
Async Programming in C# 5
Pratik Khasnabis
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
Oleg Podsechin
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian Network
Hendy Irawan
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
Rob Tweed
 
Vapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymoreVapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymore
Milan Vít
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
jeffz
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
Fulvio Corno
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
Doug Jones
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docx
aryan532920
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
 
About Node.js
About Node.jsAbout Node.js
About Node.js
Artemisa Yescas Engler
 

Similar to Concurrency - responsiveness in .NET (20)

Asynchronous in dot net4
Asynchronous in dot net4Asynchronous in dot net4
Asynchronous in dot net4
 
Web program-peformance-optimization
Web program-peformance-optimizationWeb program-peformance-optimization
Web program-peformance-optimization
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
 
15. text files
15. text files15. text files
15. text files
 
[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
 
Why async matters
Why async mattersWhy async matters
Why async matters
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Async Programming in C# 5
Async Programming in C# 5Async Programming in C# 5
Async Programming in C# 5
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian Network
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Vapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymoreVapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymore
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docx
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
About Node.js
About Node.jsAbout Node.js
About Node.js
 

More from Mårten Rånge

Know your FOSS obligations
Know your FOSS obligationsKnow your FOSS obligations
Know your FOSS obligations
Mårten Rånge
 
Ray Marching Explained
Ray Marching ExplainedRay Marching Explained
Ray Marching Explained
Mårten Rånge
 
Better performance through Superscalarity
Better performance through SuperscalarityBetter performance through Superscalarity
Better performance through Superscalarity
Mårten Rånge
 
Property Based Tesing
Property Based TesingProperty Based Tesing
Property Based Tesing
Mårten Rånge
 
Monad - a functional design pattern
Monad - a functional design patternMonad - a functional design pattern
Monad - a functional design pattern
Mårten Rånge
 
Formlets
FormletsFormlets
Formlets
Mårten Rånge
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
Mårten Rånge
 
Meta Programming
Meta ProgrammingMeta Programming
Meta Programming
Mårten Rånge
 
Concurrency scalability
Concurrency scalabilityConcurrency scalability
Concurrency scalability
Mårten Rånge
 
Concurrency
ConcurrencyConcurrency
Concurrency
Mårten Rånge
 

More from Mårten Rånge (10)

Know your FOSS obligations
Know your FOSS obligationsKnow your FOSS obligations
Know your FOSS obligations
 
Ray Marching Explained
Ray Marching ExplainedRay Marching Explained
Ray Marching Explained
 
Better performance through Superscalarity
Better performance through SuperscalarityBetter performance through Superscalarity
Better performance through Superscalarity
 
Property Based Tesing
Property Based TesingProperty Based Tesing
Property Based Tesing
 
Monad - a functional design pattern
Monad - a functional design patternMonad - a functional design pattern
Monad - a functional design pattern
 
Formlets
FormletsFormlets
Formlets
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
 
Meta Programming
Meta ProgrammingMeta Programming
Meta Programming
 
Concurrency scalability
Concurrency scalabilityConcurrency scalability
Concurrency scalability
 
Concurrency
ConcurrencyConcurrency
Concurrency
 

Recently uploaded

SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 

Recently uploaded (20)

SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 

Concurrency - responsiveness in .NET

  • 2. Mårten Rånge Ericsson AB @marten_range
  • 4.
  • 7. Three pillars of Concurrency  Scalability (CPU)  Parallel.For  Responsiveness  Task  async/await  Consistency  lock  Interlocked.*  Mutex/Event/Semaphore  Monitor
  • 9.
  • 10. string ReadSomeText (string fileName) { using (var sr = new StreamReader(fileName)) { var result = sr.ReadToEnd (); return result; } } // Blocks the thread until IO completes var text = ReadSomeText ("SomeText.txt");
  • 11. Asyncronous programming allows programs to be responsive
  • 12. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 13. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 14. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 15. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var first = await sr.ReadLineAsync(); var second = await sr.ReadLineAsync(); var third = await sr.ReadLineAsync(); return first + second + third; } }
  • 16. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 18. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 20. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 23. Your new ”best” friends  SynchronizationContext  Console apps  Continues on ThreadPool thread  WindowsFormsSynchronizationContext  Used by WindowsForms apps  Continues on the ”UI” thread  DispatcherSynchronizationContext  Used by WPF and ASP.NET apps  Continues on the ”same” thread
  • 25. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 26. Yes
  • 27. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 28. No
  • 29. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 30. No
  • 32. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 33. No
  • 34. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 35. Yes
  • 36. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 37. Yes
  • 38. So…
  • 39. SynchronizationContext  Is ”invisible”  Is a thread-global state  Impacts the behavior of your code significantly  As an application developer you can make assumptions  As a library developer you can’t make assumptions
  • 41. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 42. Yes
  • 43. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); --m_readingFiles; return result; } }
  • 44. No
  • 45. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); TraceThreadId (); return result; } }
  • 46. No
  • 47. async/await tries to be what we want
  • 49. What we need  Do one thing  Responsiveness  Predictable semantics  Continuation is executed by a thread-pool thread  Visibility  Thread-switching should be visible in code
  • 50. F# async // Focuses on responsiveness let gameLoop = async { // Switching to new thread is explicit do! Async.SwitchToNewThread () while true do // let! is like await in C# let! messages = fromVisual.AsyncDequeue 1000 for message in messages do processMessage message }
  • 51. C#  yield  Special support for enumerators  LINQ  Special support for SQL-like syntax  async/await  Special support for asynchronous programming
  • 52. F#  seq { for x in 0..10 -> i }  Enumerators implemented using Computation Expressions  query { for c in db.Customers do select c }  SQL-like syntax implemented using Computation Expressions  async { let! r=fromVisual.AsyncDequeue 1000 in r }  Asynchronous programming using Computation Expressions
  • 54. With async/await always consider the…
  • 55. SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext
  • 56. Mårten Rånge Ericsson AB @marten_range
  • 57. Links  Presentation  http://www.slideshare.net/martenrange/  Code  https://github.com/mrange/presentations/  DIY asynchronous workflows in F#  http://mrange.wordpress.com/2014/06/12/diy-asynchronous-workflows- in-f/  Asynchronous programming with async/await  http://msdn.microsoft.com/en-us/library/hh191443.aspx