SlideShare a Scribd company logo
Threads - Why Can't You
Just Play Nicely With Your
Memory?
many cores with java
session three
two cores
copyright 2013 Robert Burrell Donkin robertburrelldonkin.name
Pre-emptive multi-tasking operating
systems use involuntary context
switching to provide the illusion of
parallel processes even when the
hardware supports only a single thread
of execution.
Take Away from Session One
Even on a single core,
there's no escaping parallelism.
Take Away from Session Two
Two Cores on One Chip
● Share caches
○ not L1
○ perhaps L2
○ probably L3
● Communicate fast and fat
○ in comparison to IO: very fast
○ in comparison to memory: fast
○ in comparison to intra-core: slow
● Cache consistency is cheap(er)
Two Cores on Two Chip
● No share caches
○ not L1
○ not L2
○ not L3
○ share only main memory
● Communicate maybe...?
○ in comparison to IO: fast
○ in comparison to memory: so-so
○ in comparison to intra-core: very slow
● Consistency is expensive
When Memory is Shared
● Until memory is shared
○ compiler,
○ hardware and
○ operating system
○ sweep the messy details under the rug
● When memory is shared
○ the software platform
○ guarantees reasonable behaviour
○ when code plays by the rules
Some Software Strategies
● Applicable to any shared resource
○ but memory as an example is
■ convenient and
■ common
● Mutual exclusion
○ no concurrent access to critical sections
● Non-blocking algorithms
○ CAS - compare and swap
Locks
● Block threads
○ which do not hold the lock
○ from a critical section
● When thread holding the lock
○ exits the critical section
○ other threads may no longer be blocked
Reading Stale Data
● Recall
○ that caches are not always coherent
○ two cores and main memory may not agree about a
value for a memory location
● Mutual exclusion is often not enough
○ memory barrier needed
○ hardware ensures data is coherent
○ expensive
● In Java, synchronized
○ locks and
○ issues memory barrier
● Several ways to implement in hardware
● When writing a value:
○ send expected existing value
○ return actual existing value on failure
● A step along the road to transactional
memory
○ Similar to optimistic locking in an RDBMS
CAS - Compare and Swap
Take Away
Code executing on different cores uses copies
held in registers and caches, so memory
shared is likely to be incoherent unless the
program plays by the rules of the software
platform.

More Related Content

PDF
Threads - Why Can't You Just Play Nicely With Your Memory?
PDF
Introducing Mongo DB and setting up Adobe AEM6 with mongo
PDF
Introducing Apache Jackrabbit OAK
PDF
NUMA and Java Databases
PPT
Memory models
PPTX
Java one2015 - Work With Hundreds of Hot Terabytes in JVMs
PPT
Lecture2
PPT
Snooping 2
Threads - Why Can't You Just Play Nicely With Your Memory?
Introducing Mongo DB and setting up Adobe AEM6 with mongo
Introducing Apache Jackrabbit OAK
NUMA and Java Databases
Memory models
Java one2015 - Work With Hundreds of Hot Terabytes in JVMs
Lecture2
Snooping 2

What's hot (20)

PDF
Stack Frame Protection
PPTX
Active Data Stores at 30,000ft
PDF
Threads and Threads
PDF
Many Cores Java - Session One: Threads and Threads
PPT
Cache coherence
PDF
Coherence and consistency models in multiprocessor architecture
PDF
Concurrency
PPT
Snooping protocols 3
PDF
Nano-node: Intelligent Hard Drives in Large Storage Infrastructures
PDF
Apache CouchDB
PPTX
Cache coherence ppt
PDF
Caffe + H2O - By Cyprien noel
PPTX
Cache coherence
PDF
Cache coherence
PPTX
Cache design
PPTX
Mongo db cluster administration and Shredded Databases
PDF
Long Term Road Test of C*
PDF
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
PDF
If the data cannot come to the algorithm...
Stack Frame Protection
Active Data Stores at 30,000ft
Threads and Threads
Many Cores Java - Session One: Threads and Threads
Cache coherence
Coherence and consistency models in multiprocessor architecture
Concurrency
Snooping protocols 3
Nano-node: Intelligent Hard Drives in Large Storage Infrastructures
Apache CouchDB
Cache coherence ppt
Caffe + H2O - By Cyprien noel
Cache coherence
Cache coherence
Cache design
Mongo db cluster administration and Shredded Databases
Long Term Road Test of C*
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
If the data cannot come to the algorithm...
Ad

Viewers also liked (14)

PDF
Retrospectives In 10 Slides (With Notes)
PDF
Beyond the Open Rate: It's Time to Think Reach Frequency and Impact
PPT
Creating Loyalty Programs
PDF
An End to Order
PPTX
Why finding a balance between inspiration and perspiration is key to success ...
PPTX
Defining Engagement - the Problem with Opens and Clicks
PPTX
Alchemy Worx design portfolio May 2013
PDF
7 Myths of Email Marketing - Alchemy Worx
PPTX
WhichTestWon The Live Event: Subject Line Optimization Masterclass
PDF
XP In 10 slides
PDF
An End to Order (many cores with java, session two)
PPTX
Email as a Mass Media Channel
PDF
Apache Maven In 10 Slides
PPTX
E-mail Marketing 3.0 is coming: Are you ready?
Retrospectives In 10 Slides (With Notes)
Beyond the Open Rate: It's Time to Think Reach Frequency and Impact
Creating Loyalty Programs
An End to Order
Why finding a balance between inspiration and perspiration is key to success ...
Defining Engagement - the Problem with Opens and Clicks
Alchemy Worx design portfolio May 2013
7 Myths of Email Marketing - Alchemy Worx
WhichTestWon The Live Event: Subject Line Optimization Masterclass
XP In 10 slides
An End to Order (many cores with java, session two)
Email as a Mass Media Channel
Apache Maven In 10 Slides
E-mail Marketing 3.0 is coming: Are you ready?
Ad

Similar to Threads - Why Can't You Just Play Nicely With Your Memory_ (20)

PPTX
CPU Caches
PPT
Hs java open_party
PDF
Caching in
PDF
Java Concurrency in Practice
DOC
Concurrency Learning From Jdk Source
PDF
Ph.D. thesis presentation
PPT
12-6810-12.ppt
PDF
Java Memory Model
PDF
jvm/java - towards lock-free concurrency
PPT
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
PPT
Java threading 2
PDF
Cpu Caches
PDF
CPU Caches - Jamie Allen
PDF
[JavaOne 2011] Models for Concurrent Programming
PPTX
Николай Папирный Тема: "Java memory model для простых смертных"
PDF
Practical Introduction to Java Memory Model
PPT
Optimizing your java applications for multi core hardware
ODP
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
ODP
Java Memory Consistency Model - concepts and context
PDF
Concurrency Concepts in Java
CPU Caches
Hs java open_party
Caching in
Java Concurrency in Practice
Concurrency Learning From Jdk Source
Ph.D. thesis presentation
12-6810-12.ppt
Java Memory Model
jvm/java - towards lock-free concurrency
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
Java threading 2
Cpu Caches
CPU Caches - Jamie Allen
[JavaOne 2011] Models for Concurrent Programming
Николай Папирный Тема: "Java memory model для простых смертных"
Practical Introduction to Java Memory Model
Optimizing your java applications for multi core hardware
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
Java Memory Consistency Model - concepts and context
Concurrency Concepts in Java

Threads - Why Can't You Just Play Nicely With Your Memory_

  • 1. Threads - Why Can't You Just Play Nicely With Your Memory? many cores with java session three two cores copyright 2013 Robert Burrell Donkin robertburrelldonkin.name
  • 2. Pre-emptive multi-tasking operating systems use involuntary context switching to provide the illusion of parallel processes even when the hardware supports only a single thread of execution. Take Away from Session One
  • 3. Even on a single core, there's no escaping parallelism. Take Away from Session Two
  • 4. Two Cores on One Chip ● Share caches ○ not L1 ○ perhaps L2 ○ probably L3 ● Communicate fast and fat ○ in comparison to IO: very fast ○ in comparison to memory: fast ○ in comparison to intra-core: slow ● Cache consistency is cheap(er)
  • 5. Two Cores on Two Chip ● No share caches ○ not L1 ○ not L2 ○ not L3 ○ share only main memory ● Communicate maybe...? ○ in comparison to IO: fast ○ in comparison to memory: so-so ○ in comparison to intra-core: very slow ● Consistency is expensive
  • 6. When Memory is Shared ● Until memory is shared ○ compiler, ○ hardware and ○ operating system ○ sweep the messy details under the rug ● When memory is shared ○ the software platform ○ guarantees reasonable behaviour ○ when code plays by the rules
  • 7. Some Software Strategies ● Applicable to any shared resource ○ but memory as an example is ■ convenient and ■ common ● Mutual exclusion ○ no concurrent access to critical sections ● Non-blocking algorithms ○ CAS - compare and swap
  • 8. Locks ● Block threads ○ which do not hold the lock ○ from a critical section ● When thread holding the lock ○ exits the critical section ○ other threads may no longer be blocked
  • 9. Reading Stale Data ● Recall ○ that caches are not always coherent ○ two cores and main memory may not agree about a value for a memory location ● Mutual exclusion is often not enough ○ memory barrier needed ○ hardware ensures data is coherent ○ expensive ● In Java, synchronized ○ locks and ○ issues memory barrier
  • 10. ● Several ways to implement in hardware ● When writing a value: ○ send expected existing value ○ return actual existing value on failure ● A step along the road to transactional memory ○ Similar to optimistic locking in an RDBMS CAS - Compare and Swap
  • 11. Take Away Code executing on different cores uses copies held in registers and caches, so memory shared is likely to be incoherent unless the program plays by the rules of the software platform.