SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
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

NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 

Kürzlich hochgeladen (20)

NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 

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