Lecture14 - Threads and Memory Models Lecture 14 CS 501...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Threads and Memory Models Lecture 14 CS 501 5/18/2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Preliminaries Critiques Foundations of the C++ Concurrency Memory Model Boehm and Adve The Java Memory Model Manson, Pugh, and Adve HW 3 : Emulator posted online 2
Background image of page 2
Threads and shared memory Multithreaded programs allow multiple threads to run concurrently. each thread has its own local variables (stack and registers), but. .. all threads share a common view of memory (globals / statics) Commonly used to multiple cores in hardware 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Safety of optimization As deFned in Lecture 1: If, in their actual program context, the result of evaluating e ʼ cannot be distinguished from the result of evaluating e, the compiler can substitute e ʼ for e. What does this mean in a multi-threaded setting? 4
Background image of page 4
Register promotion // x is global, initially 0 void foo(int* a, int n) { for (int i = 0; i < n; ++i) x += i; } // Optimized void foo(int* a, int n) { int reg = x; for (int i = 0; i < n; ++i) reg += i; x = reg ; } 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Before optimization // Thread 1 void foo(int* a, int n) { for (int i = 0; i < n; ++i) x += i; } // Thread 2 void bar() { x = 10; ... } What happens when n == 0? 6
Background image of page 6
After optimization // Thread 1 void foo(int* a, int n) { int reg = x; for (int i = 0; i < n; ++i) reg += i; x = reg; } // Thread 2 void bar() { x = 10; ... } What happens when n == 0? 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What happened? In executions where n == 0, the compiler optimization creates a value out of thin air. Original code: x == 10 is guaranteed Optimized code: new write of x = 0 creates new result Safety is no longer maintained 8
Background image of page 8
How did we get here? C & C++ originally defned as single-threaded
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

Lecture14 - Threads and Memory Models Lecture 14 CS 501...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online