SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
Facts about multithreading
that'll keep you up at night
Guy Baron, Director Mobile @ Vonage, Codemotion Tel Aviv 2017
Wikipedia
multithreading is the ability of a central processing unit (CPU) to
execute multiple processes or threads concurrently
Advantages
• Utilize more CPU power by reducing the impact of delaying
operations such as IO operations
• If a thread cannot use all the computing resources of the CPU
(because instructions depend on each other's result), running
another thread may prevent those resources from becoming idle
Common Issues
T1 IO IO
Time
PROCESSING
Too Many Threads
T2
T1 IO IO
Time
PROCESSING CONTEXT SWITCH
Too Many Threads
T3
T2
T1 IO IO
Time
PROCESSING CONTEXT SWITCH
Too Many Threads
T4
T3
T2
T1 IO
Time
PROCESSING CONTEXT SWITCH
Too Many Threads
T5
T4
T3
T2
T1 IO
Time
PROCESSING CONTEXT SWITCH
Too Many Threads
Race condition
Race conditions arise in software when an application depends on the
sequence or timing of processes or threads for it to operate properly
T1 Memory T2
i++; int i = 0; i++;
0 0
1 0
1 1
1 1
1 2
2 2
read
Increment
write
read
write
Increment
T1 Memory T2
i++; int i = 0; i++;
0 0
1 0
1 0 0
1 1 0
1 1
1 1
read
Increment
write
read
write
Increment
Deadlock
A deadlock is a state in which each member of a group of actions, is
waiting for some other member to release a lock.
T1
T2
A B
T1
A
T2
B
T1
T2
A B
T1
T2
A B
T1
T2
A B
Priority Inversion
A problematic scenario in scheduling in which a high priority task is indirectly
preempted by a lower priority task effectively "inverting" the relative priorities of the two
tasks.
A
High priority
Med priority
Low priority
A
High priority
Med priority
Low priority
A
High priority
Med priority
Low priority
Uncommon issues
64-bit values
(Java) single write to a non-volatile long or double value is treated as
two separate writes: one to each 32-bit half.
This can result in a situation where a thread sees the first 32 bits of a
64-bit value from one write, and the second 32 bits from another write.
64-bit values
Memory
1 1 1 1 1 1 1 1
value1
2 2 2 2 2 2 2 2
value2
64-bit values
1 1 1 1
Memory
1 1 1 1 1 1 1 1
value1
2 2 2 2 2 2 2 2
value2
64-bit values
2 2 2 2
Memory
1 1 1 1 1 1 1 1
value1
2 2 2 2 2 2 2 2
value2
64-bit values
2 2 2 2 2 2 2 2
Memory
1 1 1 1 1 1 1 1
value1
2 2 2 2 2 2 2 2
value2
64-bit values
1 1 1 1 2 2 2 2
Memory
1 1 1 1 1 1 1 1
value1
2 2 2 2 2 2 2 2
value2
int x = 0;

boolean finished = false;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
Take a moment to read the code… All modern CPUs have multiple
levels of CPU caches
Cache 1
Memory
int x = 0;

boolean finished = false;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 0;

finished = false;
Cache 2
x = 0;

finished = false;
Cache 1
Memory
int x = 0;

boolean finished = false;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = false;
Cache 2
x = 0;

finished = false;
Cache 1
Memory
int x = 10;

boolean finished = false;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = false;
Cache 2
x = 0;

finished = false;
X
Cache 1
Memory
int x = 10;

boolean finished = false;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = true;
Cache 2
x = 0;

finished = false;
Cache 1
Memory
int x = 10;

boolean finished = true;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = true;
Cache 2
x = 0;

finished = false;
finished
Cache 1
Memory
int x = 10;

boolean finished = true;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = true;
Cache 2
x = 0;

finished = false;
?
Cache 1
Memory
int x = 10;

boolean finished = true;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(x);

}
x = 10;

finished = true;
Cache 2
x = 0;

finished = true;
finished
Cache 1
Memory
int x = 10;

boolean finished = true;
void executeOnCpu1() {

x = 10;

finished = true;

}
void executeOnCpu2() {

while (!finished) {}

System.out.print(0);

}
x = 10;

finished = true;
Cache 2
x = 0;

finished = true;
Take a moment to read the code…
int orderOfEvaluation() {

return f1() + f2() + f3();

}
int orderOfEvaluationExplicit?() {

return (f1() + f2()) + f3();

}
int orderOfEvaluationExplicit() {

return f1() + f2() + f3();

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return 1 + f2() + f3();

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return 1 + 1 + f3();

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return 1 + 1 + 10;

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
The compiler can
evaluate operands in
any order (C++)
int orderOfEvaluationExplicit() {

return f1() + f2() + f3();

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return f1() + f2() + 10;

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return f1() + 10 + 10;

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
int orderOfEvaluationExplicit() {

return 10 + 10 + 10;

}
class NoOrder {

int value = 1;



int f1() { return this.value; }



int f2() { return this.value; }



int f3() {

//...

this.value = 10;

//...

return this.value;

}

}
Thank you
We are hiring…

Más contenido relacionado

La actualidad más candente

Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerabilityCsw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
CanSecWest
 
Advanced cfg bypass on adobe flash player 18 defcon russia 23
Advanced cfg bypass on adobe flash player 18 defcon russia 23Advanced cfg bypass on adobe flash player 18 defcon russia 23
Advanced cfg bypass on adobe flash player 18 defcon russia 23
DefconRussia
 
Csw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemesCsw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemes
CanSecWest
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 

La actualidad más candente (20)

Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
Конверсия управляемых языков в неуправляемые
Конверсия управляемых языков в неуправляемыеКонверсия управляемых языков в неуправляемые
Конверсия управляемых языков в неуправляемые
 
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerabilityCsw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4
 
System Calls
System CallsSystem Calls
System Calls
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON China
 
Advanced cfg bypass on adobe flash player 18 defcon russia 23
Advanced cfg bypass on adobe flash player 18 defcon russia 23Advanced cfg bypass on adobe flash player 18 defcon russia 23
Advanced cfg bypass on adobe flash player 18 defcon russia 23
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
 
Joel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMDJoel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMD
 
Vm ware fuzzing - defcon russia 20
Vm ware fuzzing  - defcon russia 20Vm ware fuzzing  - defcon russia 20
Vm ware fuzzing - defcon russia 20
 
The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184
 
Csw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemesCsw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemes
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
 
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
A look into the sanitizer family (ASAN & UBSAN) by Akul PillaiA look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
What the &~#@<!? (Pointers in Rust)
What the &~#@<!? (Pointers in Rust)What the &~#@<!? (Pointers in Rust)
What the &~#@<!? (Pointers in Rust)
 

Similar a Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage

how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
Miller Lee
 

Similar a Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage (20)

how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
06.Loops
06.Loops06.Loops
06.Loops
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Austin c-c++-meetup-feb2018-spectre
Austin c-c++-meetup-feb2018-spectreAustin c-c++-meetup-feb2018-spectre
Austin c-c++-meetup-feb2018-spectre
 
Staging driver sins
Staging driver sinsStaging driver sins
Staging driver sins
 
The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 
[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++
 
Ch4
Ch4Ch4
Ch4
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 

Más de Codemotion Tel Aviv

Más de Codemotion Tel Aviv (20)

Keynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBMKeynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBM
 
Angular is one fire(base)! - Shmuela Jacobs
Angular is one fire(base)! - Shmuela JacobsAngular is one fire(base)! - Shmuela Jacobs
Angular is one fire(base)! - Shmuela Jacobs
 
Demystifying docker networking black magic - Lorenzo Fontana, Kiratech
Demystifying docker networking black magic - Lorenzo Fontana, KiratechDemystifying docker networking black magic - Lorenzo Fontana, Kiratech
Demystifying docker networking black magic - Lorenzo Fontana, Kiratech
 
Faster deep learning solutions from training to inference - Amitai Armon & Ni...
Faster deep learning solutions from training to inference - Amitai Armon & Ni...Faster deep learning solutions from training to inference - Amitai Armon & Ni...
Faster deep learning solutions from training to inference - Amitai Armon & Ni...
 
Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...
Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...
Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...
 
Unleash the power of angular Reactive Forms - Nir Kaufman, 500Tech
Unleash the power of angular Reactive Forms - Nir Kaufman, 500TechUnleash the power of angular Reactive Forms - Nir Kaufman, 500Tech
Unleash the power of angular Reactive Forms - Nir Kaufman, 500Tech
 
Can we build an Azure IoT controlled device in less than 40 minutes that cost...
Can we build an Azure IoT controlled device in less than 40 minutes that cost...Can we build an Azure IoT controlled device in less than 40 minutes that cost...
Can we build an Azure IoT controlled device in less than 40 minutes that cost...
 
Actors and Microservices - Can two walk together? - Rotem Hermon, Gigya
Actors and Microservices - Can two walk together? - Rotem Hermon, GigyaActors and Microservices - Can two walk together? - Rotem Hermon, Gigya
Actors and Microservices - Can two walk together? - Rotem Hermon, Gigya
 
How to Leverage Machine Learning (R, Hadoop, Spark, H2O) for Real Time Proces...
How to Leverage Machine Learning (R, Hadoop, Spark, H2O) for Real Time Proces...How to Leverage Machine Learning (R, Hadoop, Spark, H2O) for Real Time Proces...
How to Leverage Machine Learning (R, Hadoop, Spark, H2O) for Real Time Proces...
 
My Minecraft Smart Home: Prototyping the internet of uncanny things - Sascha ...
My Minecraft Smart Home: Prototyping the internet of uncanny things - Sascha ...My Minecraft Smart Home: Prototyping the internet of uncanny things - Sascha ...
My Minecraft Smart Home: Prototyping the internet of uncanny things - Sascha ...
 
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG TorinoDistributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
 
Containerised ASP.NET Core apps with Kubernetes
Containerised ASP.NET Core apps with KubernetesContainerised ASP.NET Core apps with Kubernetes
Containerised ASP.NET Core apps with Kubernetes
 
Fullstack DDD with ASP.NET Core and Anguar 2 - Ronald Harmsen, NForza
Fullstack DDD with ASP.NET Core and Anguar 2 - Ronald Harmsen, NForzaFullstack DDD with ASP.NET Core and Anguar 2 - Ronald Harmsen, NForza
Fullstack DDD with ASP.NET Core and Anguar 2 - Ronald Harmsen, NForza
 
The Art of Decomposing Monoliths - Kfir Bloch, Wix
The Art of Decomposing Monoliths - Kfir Bloch, WixThe Art of Decomposing Monoliths - Kfir Bloch, Wix
The Art of Decomposing Monoliths - Kfir Bloch, Wix
 
SOA Lessons Learnt (or Microservices done Better) - Sean Farmar, Particular S...
SOA Lessons Learnt (or Microservices done Better) - Sean Farmar, Particular S...SOA Lessons Learnt (or Microservices done Better) - Sean Farmar, Particular S...
SOA Lessons Learnt (or Microservices done Better) - Sean Farmar, Particular S...
 
S3, Cassandra or Outer Space? Dumping Time Series Data using Spark - Demi Ben...
S3, Cassandra or Outer Space? Dumping Time Series Data using Spark - Demi Ben...S3, Cassandra or Outer Space? Dumping Time Series Data using Spark - Demi Ben...
S3, Cassandra or Outer Space? Dumping Time Series Data using Spark - Demi Ben...
 
Getting Physical with Web Bluetooth - Uri Shaked, BlackBerry
Getting Physical with Web Bluetooth - Uri Shaked, BlackBerryGetting Physical with Web Bluetooth - Uri Shaked, BlackBerry
Getting Physical with Web Bluetooth - Uri Shaked, BlackBerry
 
Web based virtual reality - Tanay Pant, Mozilla
Web based virtual reality - Tanay Pant, MozillaWeb based virtual reality - Tanay Pant, Mozilla
Web based virtual reality - Tanay Pant, Mozilla
 
Material Design Demytified - Ran Nachmany, Google
Material Design Demytified - Ran Nachmany, GoogleMaterial Design Demytified - Ran Nachmany, Google
Material Design Demytified - Ran Nachmany, Google
 
All the reasons for choosing react js that you didn't know about - Avi Marcus...
All the reasons for choosing react js that you didn't know about - Avi Marcus...All the reasons for choosing react js that you didn't know about - Avi Marcus...
All the reasons for choosing react js that you didn't know about - Avi Marcus...
 

Último

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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage

  • 1. Facts about multithreading that'll keep you up at night Guy Baron, Director Mobile @ Vonage, Codemotion Tel Aviv 2017
  • 2. Wikipedia multithreading is the ability of a central processing unit (CPU) to execute multiple processes or threads concurrently
  • 3. Advantages • Utilize more CPU power by reducing the impact of delaying operations such as IO operations • If a thread cannot use all the computing resources of the CPU (because instructions depend on each other's result), running another thread may prevent those resources from becoming idle
  • 6. T2 T1 IO IO Time PROCESSING CONTEXT SWITCH Too Many Threads
  • 7. T3 T2 T1 IO IO Time PROCESSING CONTEXT SWITCH Too Many Threads
  • 8. T4 T3 T2 T1 IO Time PROCESSING CONTEXT SWITCH Too Many Threads
  • 10. Race condition Race conditions arise in software when an application depends on the sequence or timing of processes or threads for it to operate properly
  • 11. T1 Memory T2 i++; int i = 0; i++; 0 0 1 0 1 1 1 1 1 2 2 2 read Increment write read write Increment
  • 12. T1 Memory T2 i++; int i = 0; i++; 0 0 1 0 1 0 0 1 1 0 1 1 1 1 read Increment write read write Increment
  • 13. Deadlock A deadlock is a state in which each member of a group of actions, is waiting for some other member to release a lock.
  • 19.
  • 20. Priority Inversion A problematic scenario in scheduling in which a high priority task is indirectly preempted by a lower priority task effectively "inverting" the relative priorities of the two tasks.
  • 24.
  • 26. 64-bit values (Java) single write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64-bit value from one write, and the second 32 bits from another write.
  • 27. 64-bit values Memory 1 1 1 1 1 1 1 1 value1 2 2 2 2 2 2 2 2 value2
  • 28. 64-bit values 1 1 1 1 Memory 1 1 1 1 1 1 1 1 value1 2 2 2 2 2 2 2 2 value2
  • 29. 64-bit values 2 2 2 2 Memory 1 1 1 1 1 1 1 1 value1 2 2 2 2 2 2 2 2 value2
  • 30. 64-bit values 2 2 2 2 2 2 2 2 Memory 1 1 1 1 1 1 1 1 value1 2 2 2 2 2 2 2 2 value2
  • 31. 64-bit values 1 1 1 1 2 2 2 2 Memory 1 1 1 1 1 1 1 1 value1 2 2 2 2 2 2 2 2 value2
  • 32. int x = 0;
 boolean finished = false; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } Take a moment to read the code… All modern CPUs have multiple levels of CPU caches
  • 33. Cache 1 Memory int x = 0;
 boolean finished = false; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 0;
 finished = false; Cache 2 x = 0;
 finished = false;
  • 34. Cache 1 Memory int x = 0;
 boolean finished = false; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = false; Cache 2 x = 0;
 finished = false;
  • 35. Cache 1 Memory int x = 10;
 boolean finished = false; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = false; Cache 2 x = 0;
 finished = false; X
  • 36. Cache 1 Memory int x = 10;
 boolean finished = false; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = true; Cache 2 x = 0;
 finished = false;
  • 37. Cache 1 Memory int x = 10;
 boolean finished = true; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = true; Cache 2 x = 0;
 finished = false; finished
  • 38. Cache 1 Memory int x = 10;
 boolean finished = true; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = true; Cache 2 x = 0;
 finished = false; ?
  • 39. Cache 1 Memory int x = 10;
 boolean finished = true; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(x);
 } x = 10;
 finished = true; Cache 2 x = 0;
 finished = true; finished
  • 40. Cache 1 Memory int x = 10;
 boolean finished = true; void executeOnCpu1() {
 x = 10;
 finished = true;
 } void executeOnCpu2() {
 while (!finished) {}
 System.out.print(0);
 } x = 10;
 finished = true; Cache 2 x = 0;
 finished = true;
  • 41. Take a moment to read the code… int orderOfEvaluation() {
 return f1() + f2() + f3();
 } int orderOfEvaluationExplicit?() {
 return (f1() + f2()) + f3();
 }
  • 42. int orderOfEvaluationExplicit() {
 return f1() + f2() + f3();
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 43. int orderOfEvaluationExplicit() {
 return 1 + f2() + f3();
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 44. int orderOfEvaluationExplicit() {
 return 1 + 1 + f3();
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 45. int orderOfEvaluationExplicit() {
 return 1 + 1 + 10;
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 } The compiler can evaluate operands in any order (C++)
  • 46. int orderOfEvaluationExplicit() {
 return f1() + f2() + f3();
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 47. int orderOfEvaluationExplicit() {
 return f1() + f2() + 10;
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 48. int orderOfEvaluationExplicit() {
 return f1() + 10 + 10;
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 49. int orderOfEvaluationExplicit() {
 return 10 + 10 + 10;
 } class NoOrder {
 int value = 1;
 
 int f1() { return this.value; }
 
 int f2() { return this.value; }
 
 int f3() {
 //...
 this.value = 10;
 //...
 return this.value;
 }
 }
  • 50. Thank you We are hiring…