SlideShare ist ein Scribd-Unternehmen logo
1 von 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!

Weitere ähnliche Inhalte

Ähnlich wie ELEKS Summer School 2012: .NET 06 - Multithreading

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 JesenovecEESTEC LC Trieste
 
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 2016Marco Breveglieri
 
Scalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJSScalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJSAndhy Koesnandar
 
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 4James Johnson
 
MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4James Johnson
 
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 TutorialYoung-Ho Kim
 
Ostd.ksplice.talk
Ostd.ksplice.talkOstd.ksplice.talk
Ostd.ksplice.talkUdo Seidel
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara AnjargolianHakka Labs
 
Java Future S Ritter
Java Future S RitterJava Future S Ritter
Java Future S Rittercatherinewall
 
Java jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mJava jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mSteve Elliott
 
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 FirestarterMithun T. Dhar
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with ReduxFITC
 
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 VIHari Christian
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS MeetupLINAGORA
 
Down With JavaScript!
Down With JavaScript!Down With JavaScript!
Down With JavaScript!Garth Gilmour
 
Mock Objects from Concept to Code
Mock Objects from Concept to CodeMock Objects from Concept to Code
Mock Objects from Concept to CodeRob Myers
 
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 DominoFrank van der Linden
 
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®Serdar Basegmez
 

Ähnlich wie 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®
 

Mehr von Yuriy Guts

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)Yuriy Guts
 
Automated Machine Learning
Automated Machine LearningAutomated Machine Learning
Automated Machine LearningYuriy Guts
 
Target Leakage in Machine Learning
Target Leakage in Machine LearningTarget Leakage in Machine Learning
Target Leakage in Machine LearningYuriy Guts
 
Paraphrase Detection in NLP
Paraphrase Detection in NLPParaphrase Detection in NLP
Paraphrase Detection in NLPYuriy Guts
 
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 2Yuriy Guts
 
NoSQL (ELEKS DevTalks #1 - Jan 2015)
NoSQL (ELEKS DevTalks #1 - Jan 2015)NoSQL (ELEKS DevTalks #1 - Jan 2015)
NoSQL (ELEKS DevTalks #1 - Jan 2015)Yuriy Guts
 
A Developer Overview of Redis
A Developer Overview of RedisA Developer Overview of Redis
A Developer Overview of RedisYuriy Guts
 
[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 ScalaYuriy Guts
 
Redis for .NET Developers
Redis for .NET DevelopersRedis for .NET Developers
Redis for .NET DevelopersYuriy Guts
 
Aspect-Oriented Programming (AOP) in .NET
Aspect-Oriented Programming (AOP) in .NETAspect-Oriented Programming (AOP) in .NET
Aspect-Oriented Programming (AOP) in .NETYuriy Guts
 
Non-Functional Requirements
Non-Functional RequirementsNon-Functional Requirements
Non-Functional RequirementsYuriy Guts
 
Introduction to Software Architecture
Introduction to Software ArchitectureIntroduction to Software Architecture
Introduction to Software ArchitectureYuriy Guts
 
UML for Business Analysts
UML for Business AnalystsUML for Business Analysts
UML for Business AnalystsYuriy 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 AudienceYuriy Guts
 
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 CourseYuriy Guts
 
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 - DatabasesYuriy Guts
 

Mehr von 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
 

Kürzlich hochgeladen

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Kürzlich hochgeladen (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

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