SlideShare a Scribd company logo
1 of 42
Summer School 2012




Multithreading
        Yuriy Guts
       R&D Engineer

    yuriy.guts@eleks.com
Summer School 2012




“   Although threads seem to be a small step from sequential computation,
    in fact, they represent a huge step.

    They discard the most essential and appealing properties of sequential
    computation: understandability, predictability, and determinism.

    Threads, as a model of computation, are wildly nondeterministic, and
    the job of the programmer becomes one of pruning that nondeterminism.
                                                                              ”
                                           Edward A. Lee, UC Berkeley, 2006
Summer School 2012




History: so why did we need threads?




           • CPU virtualization
           • Increased robustness
           • Quazi-multitasking
Summer School 2012




Process as a resource isolation unit
Summer School 2012




 Concurrent execution
    does not exist
on a single logical CPU!
Summer School 2012




Context Switching
Summer School 2012




                      Scheduling
• Processes are not scheduled. Threads are.
• 32 different thread priority levels!

             Thread                               Process
        1.   Idle                            1.   Idle
        2.   Lowest                          2.   Below Normal
        3.   Below Normal                    3.   Normal
        4.   Normal                          4.   Above Normal
        5.   Above Normal                    5.   High
        6.   Highest                         6.   Realtime
        7.   Time-Critical
Summer School 2012




                Thread Overhead
•   Thread Kernel Object
•   Thread Environment Block (TEB)
•   User-Mode Stack [1 MB]
•   Kernel-Mode Stack [12 / 24 KB]
•   DLL: Attach/Detach Notifications
Summer School 2012



Moore’s Law
Summer School 2012




Threads for parallelization?
Summer School 2012




           Multiple Logical CPUs
•   Multi-socket motherboards
•   Single-core CPUs with Hyper Threading (HT)
•   Multi-core CPUs
•   Multi-core CPUs with per-core HT
Summer School 2012




      Thread Operations




Compute-bound                    I/O-bound
Summer School 2012




Compute-bound Operations
static void Main(string[] args)
{
    Thread workerThread = new Thread(Factorial);
    workerThread.Start(10);
    workerThread.Join();
    Console.ReadKey(true);
}

static void Factorial(object arg)
{
    int n = (int)arg;
    int result = 1;
    for (int i = 2; i <= n; i++) result *= i;
    Console.WriteLine(result);
}
Summer School 2012




Thread Lifecycle
Summer School 2012




       Too many threads = too bad
•   Kernel object overhead
•   Memory overhead
•   Context switch overhead
•   GC overhead

    Use threads wisely!
Summer School 2012




                Thread Pool
• Designed for background processing
• Self-balancing
• Avoids much overhead
Summer School 2012




Thread Pool: Compute-bound
static void Main(string[] args)
{
    ThreadPool.QueueUserWorkItem(Factorial, 10);
    Console.ReadKey(true);
}

static void Factorial(object n)
{
    int x = (int)n;
    int result = 1;
    for (int i = 2; i <= x; i++) result *= i;
    Console.WriteLine(result);
}
Summer School 2012




              Self-Balancing




Too few threads:                  Too many threads:
 Allocate more                  Remove excessive ones
Summer School 2012




Task-Based Threading




     “Work Stealing” Principle!
Summer School 2012




     Tasks: Compute-bound
static void Main(string[] args)
{
    Task<Int32> task = new Task<Int32>(Factorial, 10);
    task.Start();
    task.Wait();
    Console.WriteLine("The Sum is: " + task.Result);
    Console.ReadKey(true);
}

static int Factorial(object n)
{
    int x = (int)n;
    int result = 1;
    for (int i = 2; i <= x; i++) result *= i;
    return result;
}
Summer School 2012




                     Tasks 101
•   Each task can have child tasks…
•   …and, therefore, throw multiple exceptions
•   “Task” does not mean “separate thread”!
•   Task templating via TaskFactory
•   Custom scheduling via TaskScheduler
•   Cooperative cancellation via CancellationToken
•   Continuations, WaitAny, WaitAll, …
Summer School 2012




       Tasks: Continuations
static void Main(string[] args)

{

    Task<Int32> task = new Task<Int32>(Factorial, 10);

    task.ContinueWith(t => Console.WriteLine("Completed"),

         TaskContinuationOptions.OnlyOnRanToCompletion);

    task.ContinueWith(t => Console.WriteLine("Canceled"),

         TaskContinuationOptions.OnlyOnCanceled);

    task.ContinueWith(t => Console.WriteLine("Error"),

         TaskContinuationOptions.OnlyOnFaulted);

    task.Start();

}
Summer School 2012




Parallel LINQ (PLINQ)




     enumerable.AsParallel()
Summer School 2012




  Parallel Computations via PLINQ
static void Main(string[] args)
{
    Parallel.For(1, 10, n => Console.WriteLine("{0}: {1}", n, Factorial(n)));
    Enumerable.Range(1, 9).AsParallel().ForAll(n => /* Console... */);
    new int [] { 1, 2, 3 }.AsParallel().ForAll(n => /* Console... */);
}
Summer School 2012




                           .AsParallel()
                 does not mean faster execution
• Possible GC pressure ( i => new { ... } )
• Item acquisition and delegate execution are implemented in virtual methods
• You have to benchmark every particular situation
Summer School 2012




Synchronous I/O
Summer School 2012




Async I/O
Summer School 2012




               Async I/O Benefits
•   Less threads
•   Less GC
•   Faster debugging
•   Faster I/O if made parallel
•   Responsive UI (a must for WinPhone & Silverlight)
Summer School 2012




Asynchronous Programming Model (APM)
Implemented by:
•   Streams
•   Sockets
•   Serial Ports
•   SQL Commands
•   DNS Requests
•   Web Services
    …etc.
Summer School 2012




The Async Model Soup




 Which one to follow? Stop the madness!
Summer School 2012




New APM in .NET 4.5 / C# vNext
private byte[] GetURLContents(string url)
{
    var content = new MemoryStream();
    var webReq = (HttpWebRequest)WebRequest.Create(url);
    using (var response = webReq.GetResponse())
        using (Stream responseStream = response.GetResponseStream())
            responseStream.CopyTo(content);
    return content.ToArray();
}
private async Task<byte[]> GetURLContentsAsync(string url)
{
    var content = new MemoryStream();
    var webReq = (HttpWebRequest)WebRequest.Create(url);
    using (WebResponse response = await webReq.GetResponseAsync())
        using (Stream responseStream = response.GetResponseStream())
            await responseStream.CopyToAsync(content);
    return content.ToArray();
}
Summer School 2012




Threads and Shared State
Summer School 2012




Threads and Race Conditions
Summer School 2012




Synchronization Constructs
User-Mode
• Volatile Read/Write
• Interlocked
Kernel-Mode
• Events
• Semaphores
• …and everything derived from them

Hybrid
• Double-checked locking
• Many others…
Summer School 2012




      To make things even worse
• Language Compiler optimizations
• JIT Compiler optimizations
• CPU optimizations
  – Out-of-order execution
  – Branch prediction
  – Memory barriers
Summer School 2012




    Atomic (Interlocked) Operations
•   Atomic Swap
•   Test-and-Set
•   Compare-and-Swap
•   Fetch-and-Add
•   Load-Link / Store-Conditional
Summer School 2012




          Kernel-mode Constructs
•   WaitHandle (base class)
•   AutoResetEvent
•   ManualResetEvent
•   CountdownEvent
•   Semaphore
•   Mutex
Summer School 2012




Hybrid constructs: double-checked locking
internal sealed class Singleton {
    private static readonly Object s_lock = new Object();
    private static Singleton s_value = null;

    private Singleton() { }

    public static Singleton GetSingleton() {
         if (s_value != null) return s_value;
         Monitor.Enter(s_lock);
         if (s_value == null) {
             Singleton temp = new Singleton();
             Interlocked.Exchange(ref s_value, temp);
         }
         Monitor.Exit(s_lock);
         return s_value;
     }
}
Summer School 2012




         Concurrent Collections
• BlockingCollection<T>
• ConcurrentBag<T>
• ConcurrentDictionary<K, V>
• ConcurrentQueue<T>
• ConcurrentStack<T>
Summer School 2012




      Building Scalable Applications

•   Avoid thread blocking
•   Avoid shared state
•   Avoid statics
•   Avoid mutable structures
Summer School 2012




   ??      ?
  Q&A
yuriy.guts@eleks.com
Summer School 2012




Thank you!

More Related Content

Similar to ELEKS Summer School 2012: .NET 06 - Multithreading

Java jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mJava jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3m
Steve Elliott
 
Mock Objects from Concept to Code
Mock Objects from Concept to CodeMock Objects from Concept to Code
Mock Objects from Concept to Code
Rob Myers
 

Similar to ELEKS Summer School 2012: .NET 06 - Multithreading (20)

EESTEC Summer School 2012 - java script & jquery - Matic Jesenovec
EESTEC Summer School 2012 - java script & jquery - Matic JesenovecEESTEC Summer School 2012 - java script & jquery - Matic Jesenovec
EESTEC Summer School 2012 - java script & jquery - Matic Jesenovec
 
Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Scalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJSScalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJS
 
ASP.NET MVC and Entity Framework 4
ASP.NET MVC and Entity Framework 4ASP.NET MVC and Entity Framework 4
ASP.NET MVC and Entity Framework 4
 
MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
 
A Quick and Dirty D3.js Tutorial
A Quick and Dirty D3.js TutorialA Quick and Dirty D3.js Tutorial
A Quick and Dirty D3.js Tutorial
 
Ostd.ksplice.talk
Ostd.ksplice.talkOstd.ksplice.talk
Ostd.ksplice.talk
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara Anjargolian
 
Java Future S Ritter
Java Future S RitterJava Future S Ritter
Java Future S Ritter
 
Java jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mJava jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3m
 
Building High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 FirestarterBuilding High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 Firestarter
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
Down With JavaScript!
Down With JavaScript!Down With JavaScript!
Down With JavaScript!
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
 
Mock Objects from Concept to Code
Mock Objects from Concept to CodeMock Objects from Concept to Code
Mock Objects from Concept to Code
 
An XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on DominoAn XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on Domino
 
ICONUK 2013 - An XPager's Guide to Process Server-Side Jobs on IBM® Domino®
ICONUK 2013 - An XPager's Guide to Process Server-Side Jobs on IBM® Domino®ICONUK 2013 - An XPager's Guide to Process Server-Side Jobs on IBM® Domino®
ICONUK 2013 - An XPager's Guide to Process Server-Side Jobs on IBM® Domino®
 

More from Yuriy Guts

A Developer Overview of Redis
A Developer Overview of RedisA Developer Overview of Redis
A Developer Overview of Redis
Yuriy Guts
 
Non-Functional Requirements
Non-Functional RequirementsNon-Functional Requirements
Non-Functional Requirements
Yuriy Guts
 
Introduction to Software Architecture
Introduction to Software ArchitectureIntroduction to Software Architecture
Introduction to Software Architecture
Yuriy Guts
 
UML for Business Analysts
UML for Business AnalystsUML for Business Analysts
UML for Business Analysts
Yuriy Guts
 
Intro to Software Engineering for non-IT Audience
Intro to Software Engineering for non-IT AudienceIntro to Software Engineering for non-IT Audience
Intro to Software Engineering for non-IT Audience
Yuriy Guts
 

More from Yuriy Guts (16)

Target Leakage in Machine Learning (ODSC East 2020)
Target Leakage in Machine Learning (ODSC East 2020)Target Leakage in Machine Learning (ODSC East 2020)
Target Leakage in Machine Learning (ODSC East 2020)
 
Automated Machine Learning
Automated Machine LearningAutomated Machine Learning
Automated Machine Learning
 
Target Leakage in Machine Learning
Target Leakage in Machine LearningTarget Leakage in Machine Learning
Target Leakage in Machine Learning
 
Paraphrase Detection in NLP
Paraphrase Detection in NLPParaphrase Detection in NLP
Paraphrase Detection in NLP
 
UCU NLP Summer Workshops 2017 - Part 2
UCU NLP Summer Workshops 2017 - Part 2UCU NLP Summer Workshops 2017 - Part 2
UCU NLP Summer Workshops 2017 - Part 2
 
NoSQL (ELEKS DevTalks #1 - Jan 2015)
NoSQL (ELEKS DevTalks #1 - Jan 2015)NoSQL (ELEKS DevTalks #1 - Jan 2015)
NoSQL (ELEKS DevTalks #1 - Jan 2015)
 
A Developer Overview of Redis
A Developer Overview of RedisA Developer Overview of Redis
A Developer Overview of Redis
 
[JEEConf 2015] Lessons from Building a Modern B2C System in Scala
[JEEConf 2015] Lessons from Building a Modern B2C System in Scala[JEEConf 2015] Lessons from Building a Modern B2C System in Scala
[JEEConf 2015] Lessons from Building a Modern B2C System in Scala
 
Redis for .NET Developers
Redis for .NET DevelopersRedis for .NET Developers
Redis for .NET Developers
 
Aspect-Oriented Programming (AOP) in .NET
Aspect-Oriented Programming (AOP) in .NETAspect-Oriented Programming (AOP) in .NET
Aspect-Oriented Programming (AOP) in .NET
 
Non-Functional Requirements
Non-Functional RequirementsNon-Functional Requirements
Non-Functional Requirements
 
Introduction to Software Architecture
Introduction to Software ArchitectureIntroduction to Software Architecture
Introduction to Software Architecture
 
UML for Business Analysts
UML for Business AnalystsUML for Business Analysts
UML for Business Analysts
 
Intro to Software Engineering for non-IT Audience
Intro to Software Engineering for non-IT AudienceIntro to Software Engineering for non-IT Audience
Intro to Software Engineering for non-IT Audience
 
ELEKS DevTalks #4: Amazon Web Services Crash Course
ELEKS DevTalks #4: Amazon Web Services Crash CourseELEKS DevTalks #4: Amazon Web Services Crash Course
ELEKS DevTalks #4: Amazon Web Services Crash Course
 
ELEKS Summer School 2012: .NET 09 - Databases
ELEKS Summer School 2012: .NET 09 - DatabasesELEKS Summer School 2012: .NET 09 - Databases
ELEKS Summer School 2012: .NET 09 - Databases
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

ELEKS Summer School 2012: .NET 06 - Multithreading

  • 1. Summer School 2012 Multithreading Yuriy Guts R&D Engineer yuriy.guts@eleks.com
  • 2. Summer School 2012 “ Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism. Threads, as a model of computation, are wildly nondeterministic, and the job of the programmer becomes one of pruning that nondeterminism. ” Edward A. Lee, UC Berkeley, 2006
  • 3. Summer School 2012 History: so why did we need threads? • CPU virtualization • Increased robustness • Quazi-multitasking
  • 4. Summer School 2012 Process as a resource isolation unit
  • 5. Summer School 2012 Concurrent execution does not exist on a single logical CPU!
  • 7. Summer School 2012 Scheduling • Processes are not scheduled. Threads are. • 32 different thread priority levels! Thread Process 1. Idle 1. Idle 2. Lowest 2. Below Normal 3. Below Normal 3. Normal 4. Normal 4. Above Normal 5. Above Normal 5. High 6. Highest 6. Realtime 7. Time-Critical
  • 8. Summer School 2012 Thread Overhead • Thread Kernel Object • Thread Environment Block (TEB) • User-Mode Stack [1 MB] • Kernel-Mode Stack [12 / 24 KB] • DLL: Attach/Detach Notifications
  • 10. Summer School 2012 Threads for parallelization?
  • 11. Summer School 2012 Multiple Logical CPUs • Multi-socket motherboards • Single-core CPUs with Hyper Threading (HT) • Multi-core CPUs • Multi-core CPUs with per-core HT
  • 12. Summer School 2012 Thread Operations Compute-bound I/O-bound
  • 13. Summer School 2012 Compute-bound Operations static void Main(string[] args) { Thread workerThread = new Thread(Factorial); workerThread.Start(10); workerThread.Join(); Console.ReadKey(true); } static void Factorial(object arg) { int n = (int)arg; int result = 1; for (int i = 2; i <= n; i++) result *= i; Console.WriteLine(result); }
  • 15. Summer School 2012 Too many threads = too bad • Kernel object overhead • Memory overhead • Context switch overhead • GC overhead Use threads wisely!
  • 16. Summer School 2012 Thread Pool • Designed for background processing • Self-balancing • Avoids much overhead
  • 17. Summer School 2012 Thread Pool: Compute-bound static void Main(string[] args) { ThreadPool.QueueUserWorkItem(Factorial, 10); Console.ReadKey(true); } static void Factorial(object n) { int x = (int)n; int result = 1; for (int i = 2; i <= x; i++) result *= i; Console.WriteLine(result); }
  • 18. Summer School 2012 Self-Balancing Too few threads: Too many threads: Allocate more Remove excessive ones
  • 19. Summer School 2012 Task-Based Threading “Work Stealing” Principle!
  • 20. Summer School 2012 Tasks: Compute-bound static void Main(string[] args) { Task<Int32> task = new Task<Int32>(Factorial, 10); task.Start(); task.Wait(); Console.WriteLine("The Sum is: " + task.Result); Console.ReadKey(true); } static int Factorial(object n) { int x = (int)n; int result = 1; for (int i = 2; i <= x; i++) result *= i; return result; }
  • 21. Summer School 2012 Tasks 101 • Each task can have child tasks… • …and, therefore, throw multiple exceptions • “Task” does not mean “separate thread”! • Task templating via TaskFactory • Custom scheduling via TaskScheduler • Cooperative cancellation via CancellationToken • Continuations, WaitAny, WaitAll, …
  • 22. Summer School 2012 Tasks: Continuations static void Main(string[] args) { Task<Int32> task = new Task<Int32>(Factorial, 10); task.ContinueWith(t => Console.WriteLine("Completed"), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith(t => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith(t => Console.WriteLine("Error"), TaskContinuationOptions.OnlyOnFaulted); task.Start(); }
  • 23. Summer School 2012 Parallel LINQ (PLINQ) enumerable.AsParallel()
  • 24. Summer School 2012 Parallel Computations via PLINQ static void Main(string[] args) { Parallel.For(1, 10, n => Console.WriteLine("{0}: {1}", n, Factorial(n))); Enumerable.Range(1, 9).AsParallel().ForAll(n => /* Console... */); new int [] { 1, 2, 3 }.AsParallel().ForAll(n => /* Console... */); }
  • 25. Summer School 2012 .AsParallel() does not mean faster execution • Possible GC pressure ( i => new { ... } ) • Item acquisition and delegate execution are implemented in virtual methods • You have to benchmark every particular situation
  • 28. Summer School 2012 Async I/O Benefits • Less threads • Less GC • Faster debugging • Faster I/O if made parallel • Responsive UI (a must for WinPhone & Silverlight)
  • 29. Summer School 2012 Asynchronous Programming Model (APM) Implemented by: • Streams • Sockets • Serial Ports • SQL Commands • DNS Requests • Web Services …etc.
  • 30. Summer School 2012 The Async Model Soup Which one to follow? Stop the madness!
  • 31. Summer School 2012 New APM in .NET 4.5 / C# vNext private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (var response = webReq.GetResponse()) using (Stream responseStream = response.GetResponseStream()) responseStream.CopyTo(content); return content.ToArray(); } private async Task<byte[]> GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) using (Stream responseStream = response.GetResponseStream()) await responseStream.CopyToAsync(content); return content.ToArray(); }
  • 32. Summer School 2012 Threads and Shared State
  • 33. Summer School 2012 Threads and Race Conditions
  • 34. Summer School 2012 Synchronization Constructs User-Mode • Volatile Read/Write • Interlocked Kernel-Mode • Events • Semaphores • …and everything derived from them Hybrid • Double-checked locking • Many others…
  • 35. Summer School 2012 To make things even worse • Language Compiler optimizations • JIT Compiler optimizations • CPU optimizations – Out-of-order execution – Branch prediction – Memory barriers
  • 36. Summer School 2012 Atomic (Interlocked) Operations • Atomic Swap • Test-and-Set • Compare-and-Swap • Fetch-and-Add • Load-Link / Store-Conditional
  • 37. Summer School 2012 Kernel-mode Constructs • WaitHandle (base class) • AutoResetEvent • ManualResetEvent • CountdownEvent • Semaphore • Mutex
  • 38. Summer School 2012 Hybrid constructs: double-checked locking internal sealed class Singleton { private static readonly Object s_lock = new Object(); private static Singleton s_value = null; private Singleton() { } public static Singleton GetSingleton() { if (s_value != null) return s_value; Monitor.Enter(s_lock); if (s_value == null) { Singleton temp = new Singleton(); Interlocked.Exchange(ref s_value, temp); } Monitor.Exit(s_lock); return s_value; } }
  • 39. Summer School 2012 Concurrent Collections • BlockingCollection<T> • ConcurrentBag<T> • ConcurrentDictionary<K, V> • ConcurrentQueue<T> • ConcurrentStack<T>
  • 40. Summer School 2012 Building Scalable Applications • Avoid thread blocking • Avoid shared state • Avoid statics • Avoid mutable structures
  • 41. Summer School 2012 ?? ? Q&A yuriy.guts@eleks.com