SlideShare a Scribd company logo
what every web and app developer  should know about multithreading
why
why On the desktop, one core is rarely enough On the web, sometimes we need parallel execution Performance requires caching Persistence of connectivity requires responsiveness Disk and network I/O is indispensible and v ery  slow
threads A way to execute two things at once
threads A way to execute two things almost at once Lightweight Independent execution Almost like a separate process
thread process versus
thread A process is isolated in memory When it dies, its memory is released When it dies, its threads die too Somewhat difficult to talk to other processes versus
All threads in a process share memory Can be started and stopped as needed On some platforms, cheaper to launch than a process Can be native (kernel-based) or user-mode process versus
threads
threads Less predictable execution Must control for re-entrancy of code Must be aware of shared data More difficult than it seems
synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x  set x = my_local_x + 1 Thread B my_local_x = x  set x = my_local_x + 2
synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x  set x = my_local_x + 1 Thread B my_local_x = x  set x = my_local_x + 2 If we started out with x = 2, we end up with x = 5
synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x  set x = my_local_x + 1 Thread B my_local_x = x  set x = my_local_x + 2 If we started out with x = 2, we end up with x = 5
synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x  set x = my_local_x + 1 Thread B my_local_x = x  set x = my_local_x + 2 If we started out with x = 2, we end up with x = 4
synchronization First rule of synchronization:  avoid needing it Thread-local storage Function scope variables No side effects Functional languages list.sort()
synchronization First rule of synchronization:  avoid needing it Thread-local storage Function scope variables No side effects Functional languages list.sort() newlist = list.sort()
synchronization Second rule of synchronization:  join threads Use a worker thread Join – wait for it to finish, then read its results
synchronization Thread A Thread B Main Thread Start A Start B Join A and B Read data
synchronization Third rule of synchronization:  go critical Declare a critical section You are alone within your application… … until you end the critical section Application-wide setting Hard to use when you have a bunch of threads
synchronization Third rule of synchronization:  mutual exclusion
synchronization Third rule of synchronization:  mut ual  ex clusion
synchronization Third rule of synchronization:  mutex Allows you to “lock” and “unlock” resources Like an object for a mini-critical section Thread A lock M my_local_x = x  set x = my_local_x + 1 unlock M Thread B lock M my_local_x = x  set x = my_local_x + 1 unlock M
synchronization Third rule of synchronization:  use a semaphore Like a mutex, but lets more than one thread through Mutex with a counter Checks availability, then “acquires” one count When done, “releases” one count, unblocking others
synchronization Synchronization is about blocking Allows you to control access to code and data Protects areas of code that shouldn’t be left to chance
examples Worker threads Thread pools Producer / Consumer Cache Will use .NET for examples
example
example
example
example
bad threading
bad threading It is simple when simple, and fiendish when complex Watch out for race conditions: lock often to prevent Watch for deadlocks: don’t lock too much Watch for incomplete locks: lock carefully
bad threading Lock for the smallest amount of time If possible, lock for consistency… … then copy the data and use it locally Instead of blocking on locks, wait with a timeout Use lots of debug logging if you’re in trouble
photo credits CC BY-NC-ND 2.0 – Clemens Schwaighofer – http://flickr.com/photos/gullevek/257135337/ CC BY-NC-SA 2.0 – Robert Parviainen – http://flickr.com/photos/rtv/2574427997/ CC BY-NC-ND 2.0 – Sudhir Srinivasa – http://flickr.com/photos/sudhirs/111760673/ CC BY-NC-SA 2.0 – Rick Harrison – http://flickr.com/photos/sovietuk/2657691123/ CC BY-NC-ND 2.0 – Joe Chiapputo – http://flickr.com/photos/cocoabeachjoe/1924133031/ CC BY-ND 2.0 – Craig Allen – http://flickr.com/photos/anabadili/2759448841/

More Related Content

What's hot (20)

PPT
The Pillars Of Concurrency
aviade
 
PPT
Class3
ankitasinghbsc
 
PPTX
Multithreading
sagsharma
 
PDF
Wired and Wireless Examples in ns2
Pradeep Kumar TS
 
PDF
Glusterfs session #4 call frame and programming model
Pranith Karampuri
 
PPTX
PERFORMANCE EVALUATION OF OPEN SHORTEST PATH FIRST VERSION 3 IN TERMS OF DATA...
Azrull Haziq
 
PPT
Network Security Lec4
Federal Urdu University
 
PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
 
ODP
FOSDEM 2011 - 0MQ
pieterh
 
PDF
INET for Starters
Fayruz Rahma
 
PDF
Fast dynamic analysis, Kostya Serebryany
yaevents
 
PPT
Distributed file systems (from Google)
Sri Prasanna
 
PPT
Thread safety and code syncronization
Srinivas Reddy
 
PPTX
Introduction to Exploitation
primeteacher32
 
PPTX
Information and data security block cipher and the data encryption standard (...
Mazin Alwaaly
 
PDF
Correct and efficient synchronization of java thread
outofmemoryerror
 
PPT
Des lecture
ANIKET UTTAM
 
PDF
DEC algorithm
vss gowtham
 
PPTX
Enumeration
fsciallo
 
PPTX
Block Cipher
Brandon Byungyong Jo
 
The Pillars Of Concurrency
aviade
 
Multithreading
sagsharma
 
Wired and Wireless Examples in ns2
Pradeep Kumar TS
 
Glusterfs session #4 call frame and programming model
Pranith Karampuri
 
PERFORMANCE EVALUATION OF OPEN SHORTEST PATH FIRST VERSION 3 IN TERMS OF DATA...
Azrull Haziq
 
Network Security Lec4
Federal Urdu University
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
 
FOSDEM 2011 - 0MQ
pieterh
 
INET for Starters
Fayruz Rahma
 
Fast dynamic analysis, Kostya Serebryany
yaevents
 
Distributed file systems (from Google)
Sri Prasanna
 
Thread safety and code syncronization
Srinivas Reddy
 
Introduction to Exploitation
primeteacher32
 
Information and data security block cipher and the data encryption standard (...
Mazin Alwaaly
 
Correct and efficient synchronization of java thread
outofmemoryerror
 
Des lecture
ANIKET UTTAM
 
DEC algorithm
vss gowtham
 
Enumeration
fsciallo
 
Block Cipher
Brandon Byungyong Jo
 

Similar to what every web and app developer should know about multithreading (20)

PPT
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
tugrulh
 
PPT
Lec1 Intro
mobius.cn
 
PPT
Lec1 Intro
guest9b0d21
 
PPT
Introduction & Parellelization on large scale clusters
Sri Prasanna
 
PPT
Parallel Programming: Beyond the Critical Section
Tony Albrecht
 
PPTX
Interactions complicate debugging
Syed Zaid Irshad
 
PDF
.Net Threading
Erik Ralston
 
PDF
chap7_slidesforparallelcomputingananthgrama
doomzday27
 
PPTX
Computer architecture related concepts, process
ssusera979f41
 
PPT
Introduction to Cluster Computing and Map Reduce (from Google)
Sri Prasanna
 
PPT
Processes, Threads and Scheduler
Munazza-Mah-Jabeen
 
PPTX
Operating Systems R20 Unit 2.pptx
Prudhvi668506
 
PDF
Threads are evil
Frederik Vannoote
 
PPTX
Multithreading_07451fffffffffffffffgg7.pptx
nailapp2023
 
PPTX
C# Thread synchronization
Prem Kumar Badri
 
PPT
Clojure's take on concurrency
yoavrubin
 
PDF
Our Concurrent Past; Our Distributed Future
C4Media
 
PPT
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
PPTX
Concurrency in c#
RezaHamidpour
 
ODP
Concept of thread
Munmun Das Bhowmik
 
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
tugrulh
 
Lec1 Intro
mobius.cn
 
Lec1 Intro
guest9b0d21
 
Introduction & Parellelization on large scale clusters
Sri Prasanna
 
Parallel Programming: Beyond the Critical Section
Tony Albrecht
 
Interactions complicate debugging
Syed Zaid Irshad
 
.Net Threading
Erik Ralston
 
chap7_slidesforparallelcomputingananthgrama
doomzday27
 
Computer architecture related concepts, process
ssusera979f41
 
Introduction to Cluster Computing and Map Reduce (from Google)
Sri Prasanna
 
Processes, Threads and Scheduler
Munazza-Mah-Jabeen
 
Operating Systems R20 Unit 2.pptx
Prudhvi668506
 
Threads are evil
Frederik Vannoote
 
Multithreading_07451fffffffffffffffgg7.pptx
nailapp2023
 
C# Thread synchronization
Prem Kumar Badri
 
Clojure's take on concurrency
yoavrubin
 
Our Concurrent Past; Our Distributed Future
C4Media
 
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Concurrency in c#
RezaHamidpour
 
Concept of thread
Munmun Das Bhowmik
 
Ad

Recently uploaded (20)

PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Digital Circuits, important subject in CS
contactparinay1
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Ad

what every web and app developer should know about multithreading

  • 1. what every web and app developer should know about multithreading
  • 2. why
  • 3. why On the desktop, one core is rarely enough On the web, sometimes we need parallel execution Performance requires caching Persistence of connectivity requires responsiveness Disk and network I/O is indispensible and v ery slow
  • 4. threads A way to execute two things at once
  • 5. threads A way to execute two things almost at once Lightweight Independent execution Almost like a separate process
  • 7. thread A process is isolated in memory When it dies, its memory is released When it dies, its threads die too Somewhat difficult to talk to other processes versus
  • 8. All threads in a process share memory Can be started and stopped as needed On some platforms, cheaper to launch than a process Can be native (kernel-based) or user-mode process versus
  • 10. threads Less predictable execution Must control for re-entrancy of code Must be aware of shared data More difficult than it seems
  • 11. synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x set x = my_local_x + 1 Thread B my_local_x = x set x = my_local_x + 2
  • 12. synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x set x = my_local_x + 1 Thread B my_local_x = x set x = my_local_x + 2 If we started out with x = 2, we end up with x = 5
  • 13. synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x set x = my_local_x + 1 Thread B my_local_x = x set x = my_local_x + 2 If we started out with x = 2, we end up with x = 5
  • 14. synchronization We must retain predictability in our programs Two threads fighting for the same variable Thread A my_local_x = x set x = my_local_x + 1 Thread B my_local_x = x set x = my_local_x + 2 If we started out with x = 2, we end up with x = 4
  • 15. synchronization First rule of synchronization: avoid needing it Thread-local storage Function scope variables No side effects Functional languages list.sort()
  • 16. synchronization First rule of synchronization: avoid needing it Thread-local storage Function scope variables No side effects Functional languages list.sort() newlist = list.sort()
  • 17. synchronization Second rule of synchronization: join threads Use a worker thread Join – wait for it to finish, then read its results
  • 18. synchronization Thread A Thread B Main Thread Start A Start B Join A and B Read data
  • 19. synchronization Third rule of synchronization: go critical Declare a critical section You are alone within your application… … until you end the critical section Application-wide setting Hard to use when you have a bunch of threads
  • 20. synchronization Third rule of synchronization: mutual exclusion
  • 21. synchronization Third rule of synchronization: mut ual ex clusion
  • 22. synchronization Third rule of synchronization: mutex Allows you to “lock” and “unlock” resources Like an object for a mini-critical section Thread A lock M my_local_x = x set x = my_local_x + 1 unlock M Thread B lock M my_local_x = x set x = my_local_x + 1 unlock M
  • 23. synchronization Third rule of synchronization: use a semaphore Like a mutex, but lets more than one thread through Mutex with a counter Checks availability, then “acquires” one count When done, “releases” one count, unblocking others
  • 24. synchronization Synchronization is about blocking Allows you to control access to code and data Protects areas of code that shouldn’t be left to chance
  • 25. examples Worker threads Thread pools Producer / Consumer Cache Will use .NET for examples
  • 31. bad threading It is simple when simple, and fiendish when complex Watch out for race conditions: lock often to prevent Watch for deadlocks: don’t lock too much Watch for incomplete locks: lock carefully
  • 32. bad threading Lock for the smallest amount of time If possible, lock for consistency… … then copy the data and use it locally Instead of blocking on locks, wait with a timeout Use lots of debug logging if you’re in trouble
  • 33. photo credits CC BY-NC-ND 2.0 – Clemens Schwaighofer – http://flickr.com/photos/gullevek/257135337/ CC BY-NC-SA 2.0 – Robert Parviainen – http://flickr.com/photos/rtv/2574427997/ CC BY-NC-ND 2.0 – Sudhir Srinivasa – http://flickr.com/photos/sudhirs/111760673/ CC BY-NC-SA 2.0 – Rick Harrison – http://flickr.com/photos/sovietuk/2657691123/ CC BY-NC-ND 2.0 – Joe Chiapputo – http://flickr.com/photos/cocoabeachjoe/1924133031/ CC BY-ND 2.0 – Craig Allen – http://flickr.com/photos/anabadili/2759448841/

Editor's Notes

  • #2: By-nc-nd 2.0 Clemens Schwaighofer http://flickr.com/photos/gullevek/257135337/ Introduction to threading Time for questions at the end; if it’s really brief, interrupt me