SlideShare a Scribd company logo
J e f f H a r t
V a n i s h i n g C l o u d s , I n c .
Async/Await for Fun and Profit
Multithreading is just one damn thing after…
…before, or the simultaneously with another.
Scott Meyers and Andrei Alexandrescu
…before
March 7-8, 2015
Why Multithreading?
 “Modern” apps force multithreading
 Desktop/client – avoiding the “toilet bowl”
 Server-side scalability – all about the cores
 Economics – computers not faster since P4 (90nm)
Copyright © Jeff Hart, 2015
Know Your Goal
 Offloading – free the “main” thread
 Still uses another (Thread Pool) thread
 Works for CPU bound (i.e., thread backed)
 Scaling – not using “any” thread
 Still uses IO completion ports
 Only works for IO bound provided by “framework”
Copyright © Jeff Hart, 2015
Domain: Prime Numbers
 Natural numbers c only divisible by itself and 1
for(int d=2; d<c-1; d++)…
 Only even prime is 2
for(int d=3; d<c-1; d+=2)…
 If c/d = q, then c/q = d; and q or d ≤ SQRT(c)
for(int d=3; d<=Math.Sqrt(c); d+=2)…
 If c/d, then d is a prime or divisible by one<d
So only test against previously found primes
… lots of more powerful sieves
Copyright © Jeff Hart, 2015
Basic ROT
 await converts Task<T> to <T> (Task to void)
 Using await  changing signature: async Task[<T>]
 Warning on async method w/o using await
 Can’t await in catch/finally or lock
 Can’t make properties async
 Method returns Task<T>, but you return T;
 If you have used await
 Never call async void (event handlers only)
Copyright © Jeff Hart, 2015
Floor to Ceiling
 Go down to:
 Framework XxxAsync for scaling
 “Creating” asynchronicity
Task.Run( { … } ) way better than worker threads
 Go up to:
 Handler
 MSTest, etc.
 “Eating” asynchronicity
task.ContinueWith( …, TaskContinuationOptions.Only|Not)
task.Result - blocks
Copyright © Jeff Hart, 2015
We’ve Been At This…
 CLR v1: APM – Async Programming Model
 Simple (mechanical) but limiting
 CLR v2: EAP – Event-based Asynchronous Pattern
 Very flexible but lots of “namespace noise”
 CLR v4: TAP – Task-based Asynchronous Pattern
 V4.5 async/await “complier sugar”
Copyright © Jeff Hart, 2015
Getting Real (Data)
 Entity Framework 6 is TAP enabled
 Secret: using System.Data.Entity;
 All|AnyAsync
 Count|Average|Sum|Min|MaxAsync
 ContainsAsync
 First|Single[OrDefault]Async
 ForEachAsync
 LoadAsync
 ToArray|List|DictionaryAsync
Copyright © Jeff Hart, 2015
Graduation
foreach( var d in GetLotsOfData() ){
DoSomethingReallyLong(d);
}
Parallel.ForEach( GetLotsOfData(), d=>{
DoSomethingReallyLong(d);
}
var tasks = new List<Task>()
foreach( var d in GetLotsOfData() ){
tasks.Add(DoSomethingReallyLongAsync(d));
}
Task.AwaitAll(tasks);
?
Copyright © Jeff Hart, 2015
Layers of Multithreading
 Async/await two-step
 Separating task and await
 Knowin’ when to make ‘em;
And knowin’ when to tend ‘em
 Rules of Thumb
 Mostly compiler enforced/assisted
 Avoid async void (fire and forget)
 Go “floor to ceiling” when possible
 Task’s members to “stop”
 TaskCompletionSource to “create”
The scary bit:
Synchronizing (data)
Copyright © Jeff Hart, 2015
SpeakerRate.com
• Search for “async” or “socalcodecamp”
• h t t p : / / s p e a k e r r a t e . c o m / t a l k s / 4 9 3 2 1 - s o c a l c o d e c a m p -
i n t r o - t o - a s y n c - a w a i t
Thank YOU!
Copyright © Jeff Hart, 2015
17

More Related Content

PDF
Introducing Async/Await
PDF
Spring Framework - Core
ODP
Rust Primer
PDF
C++ Unit Test with Google Testing Framework
PDF
Deep dive into Coroutines on JVM @ KotlinConf 2017
PPTX
Java presentation
PDF
Introduction to kotlin coroutines
Introducing Async/Await
Spring Framework - Core
Rust Primer
C++ Unit Test with Google Testing Framework
Deep dive into Coroutines on JVM @ KotlinConf 2017
Java presentation
Introduction to kotlin coroutines

What's hot (20)

PPTX
Async Programming in C# 5
ODP
Python unit testing
PPTX
Introduction to Spring Framework
PPTX
Web services SOAP
PPTX
Rust vs C++
PDF
Introduction to rust: a low-level language with high-level abstractions
PDF
Android Networking
PPTX
jQuery
PPTX
Async programming and python
PDF
Asynchronous JavaScript Programming
PDF
From framework coupled code to #microservices through #DDD /by @codelytv
PDF
Spring Boot
PDF
JavaScript: Variables and Functions
PDF
Spring Framework - AOP
PDF
Why rust?
PDF
An introduction to Google test framework
PPTX
Introduction to REST - API
PDF
Kotlin Coroutines Reloaded
PPTX
JavaScript Promises
PPTX
An Introduction To REST API
Async Programming in C# 5
Python unit testing
Introduction to Spring Framework
Web services SOAP
Rust vs C++
Introduction to rust: a low-level language with high-level abstractions
Android Networking
jQuery
Async programming and python
Asynchronous JavaScript Programming
From framework coupled code to #microservices through #DDD /by @codelytv
Spring Boot
JavaScript: Variables and Functions
Spring Framework - AOP
Why rust?
An introduction to Google test framework
Introduction to REST - API
Kotlin Coroutines Reloaded
JavaScript Promises
An Introduction To REST API
Ad

Viewers also liked (15)

PPTX
Async await
PPTX
Modos de transmisión
PPTX
CTU June 2011 - C# 5.0 - ASYNC & Await
PPTX
Async / Await: Programación asíncrona para dummies (12 horas visual studio)
PPTX
Async in .NET
PPTX
Asynchronous programming from Xamarin Hakcday in Melbourne
PPTX
Evolution of C# delegates
PDF
Using Async in your Mobile Apps - Marek Safar
PPTX
C#을 이용한 task 병렬화와 비동기 패턴
PPTX
transmisión de datos, detección y corrección de errores y compresión de datos
PPTX
Estructura del Internet, redes lan, man y wan, módems.
PDF
Async await...oh wait!
PDF
C# Delegates and Event Handling
PPTX
C# Delegates
PPT
Presentacion grafica de la configuarcion de una red punto a punto
Async await
Modos de transmisión
CTU June 2011 - C# 5.0 - ASYNC & Await
Async / Await: Programación asíncrona para dummies (12 horas visual studio)
Async in .NET
Asynchronous programming from Xamarin Hakcday in Melbourne
Evolution of C# delegates
Using Async in your Mobile Apps - Marek Safar
C#을 이용한 task 병렬화와 비동기 패턴
transmisión de datos, detección y corrección de errores y compresión de datos
Estructura del Internet, redes lan, man y wan, módems.
Async await...oh wait!
C# Delegates and Event Handling
C# Delegates
Presentacion grafica de la configuarcion de una red punto a punto
Ad

Similar to Async/Await (20)

PPT
Migration To Multi Core - Parallel Programming Models
PPT
Everyone loves PHP
PPT
Overview Of Parallel Development - Ericnel
PDF
Better Code: Concurrency
PPTX
C# 5 deep drive into asynchronous programming
PDF
Concurrent Programming OpenMP @ Distributed System Discussion
ODP
High-Performance Computing and OpenSolaris
PDF
parallel-computation.pdf
PDF
Programming 1 [compatibility mode]mm
PDF
Parallel computation
PPTX
C++ Coroutines
PDF
Unmanaged Parallelization via P/Invoke
PPTX
Asynchronous programming - .NET Way
PPT
Threaded Programming
PPTX
Comparison of various streaming technologies
PPTX
Apache Flink@ Strata & Hadoop World London
PDF
How to deploy & optimize eZ Publish
PDF
Introduction to HPC Programming Models - EUDAT Summer School (Stefano Markidi...
PPTX
SequenceL intro slideshare
PPTX
SequenceL Auto-Parallelizing Toolset Intro slideshare
Migration To Multi Core - Parallel Programming Models
Everyone loves PHP
Overview Of Parallel Development - Ericnel
Better Code: Concurrency
C# 5 deep drive into asynchronous programming
Concurrent Programming OpenMP @ Distributed System Discussion
High-Performance Computing and OpenSolaris
parallel-computation.pdf
Programming 1 [compatibility mode]mm
Parallel computation
C++ Coroutines
Unmanaged Parallelization via P/Invoke
Asynchronous programming - .NET Way
Threaded Programming
Comparison of various streaming technologies
Apache Flink@ Strata & Hadoop World London
How to deploy & optimize eZ Publish
Introduction to HPC Programming Models - EUDAT Summer School (Stefano Markidi...
SequenceL intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshare

Recently uploaded (20)

PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
AI in Product Development-omnex systems
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Choose the Right IT Partner for Your Business in Malaysia
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
How Creative Agencies Leverage Project Management Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
AI in Product Development-omnex systems
Operating system designcfffgfgggggggvggggggggg
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Reimagine Home Health with the Power of Agentic AI​
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Understanding Forklifts - TECH EHS Solution

Async/Await

  • 1. J e f f H a r t V a n i s h i n g C l o u d s , I n c . Async/Await for Fun and Profit Multithreading is just one damn thing after… …before, or the simultaneously with another. Scott Meyers and Andrei Alexandrescu …before March 7-8, 2015
  • 2. Why Multithreading?  “Modern” apps force multithreading  Desktop/client – avoiding the “toilet bowl”  Server-side scalability – all about the cores  Economics – computers not faster since P4 (90nm) Copyright © Jeff Hart, 2015
  • 3. Know Your Goal  Offloading – free the “main” thread  Still uses another (Thread Pool) thread  Works for CPU bound (i.e., thread backed)  Scaling – not using “any” thread  Still uses IO completion ports  Only works for IO bound provided by “framework” Copyright © Jeff Hart, 2015
  • 4. Domain: Prime Numbers  Natural numbers c only divisible by itself and 1 for(int d=2; d<c-1; d++)…  Only even prime is 2 for(int d=3; d<c-1; d+=2)…  If c/d = q, then c/q = d; and q or d ≤ SQRT(c) for(int d=3; d<=Math.Sqrt(c); d+=2)…  If c/d, then d is a prime or divisible by one<d So only test against previously found primes … lots of more powerful sieves Copyright © Jeff Hart, 2015
  • 5. Basic ROT  await converts Task<T> to <T> (Task to void)  Using await  changing signature: async Task[<T>]  Warning on async method w/o using await  Can’t await in catch/finally or lock  Can’t make properties async  Method returns Task<T>, but you return T;  If you have used await  Never call async void (event handlers only) Copyright © Jeff Hart, 2015
  • 6. Floor to Ceiling  Go down to:  Framework XxxAsync for scaling  “Creating” asynchronicity Task.Run( { … } ) way better than worker threads  Go up to:  Handler  MSTest, etc.  “Eating” asynchronicity task.ContinueWith( …, TaskContinuationOptions.Only|Not) task.Result - blocks Copyright © Jeff Hart, 2015
  • 7. We’ve Been At This…  CLR v1: APM – Async Programming Model  Simple (mechanical) but limiting  CLR v2: EAP – Event-based Asynchronous Pattern  Very flexible but lots of “namespace noise”  CLR v4: TAP – Task-based Asynchronous Pattern  V4.5 async/await “complier sugar” Copyright © Jeff Hart, 2015
  • 8. Getting Real (Data)  Entity Framework 6 is TAP enabled  Secret: using System.Data.Entity;  All|AnyAsync  Count|Average|Sum|Min|MaxAsync  ContainsAsync  First|Single[OrDefault]Async  ForEachAsync  LoadAsync  ToArray|List|DictionaryAsync Copyright © Jeff Hart, 2015
  • 9. Graduation foreach( var d in GetLotsOfData() ){ DoSomethingReallyLong(d); } Parallel.ForEach( GetLotsOfData(), d=>{ DoSomethingReallyLong(d); } var tasks = new List<Task>() foreach( var d in GetLotsOfData() ){ tasks.Add(DoSomethingReallyLongAsync(d)); } Task.AwaitAll(tasks); ? Copyright © Jeff Hart, 2015
  • 10. Layers of Multithreading  Async/await two-step  Separating task and await  Knowin’ when to make ‘em; And knowin’ when to tend ‘em  Rules of Thumb  Mostly compiler enforced/assisted  Avoid async void (fire and forget)  Go “floor to ceiling” when possible  Task’s members to “stop”  TaskCompletionSource to “create” The scary bit: Synchronizing (data) Copyright © Jeff Hart, 2015
  • 11. SpeakerRate.com • Search for “async” or “socalcodecamp” • h t t p : / / s p e a k e r r a t e . c o m / t a l k s / 4 9 3 2 1 - s o c a l c o d e c a m p - i n t r o - t o - a s y n c - a w a i t Thank YOU! Copyright © Jeff Hart, 2015 17