Homework 4

advertisement

Design of Parallel and High Performance Computing

HS 2011

Department of Computer Science

ETH Zurich

Homework 4

Out: 2011-11-03

Due: 2011-11-09

Revision : 1

1 Java Memory Model Examples

The objective of this assignment is get familiar with the Java Memory Model (JMM). Take a look at the subsequent examples and answer the questions in the context of the JMM. If your answer is yes, show a sequence of code transformations that result in that behavior. If the answer is no, please state why the behavior is disallowed. The non-volatile variables x , y , and z are shared between multiple threads. The volatile variables v1 , v2 are shared between threads. Registers r1 , r2 , and r3 are local variables, and initially 0.

Example 1: Is r1 == r2 == r3 == 1 allowed in the JMM?

Initially, x == y == 0

Thread 1 Thread 2

1: r1 = x 4: r3 = y

2: r2 = r1 | 1 5: x = r3

3: y = r2

Example 2: Is r1 == r3 == 1, r2 == r4 == 0 allowed in the JMM?

Initially, v1 == v2 == 0

Thread 1 Thread 2 Thread 3 Thread 4

1: v1 = 1 2: v2 = 1 3: r1 = v1 5: r3 = v2

4: r2 = v2 6: r4 = v1

Example 3: Is r1 == r2 == r3 == 0 allowed in the JMM?

Initially, x == y == z == 0

Thread 1 Thread 2 Thread 3 Thread 4

1: z = 1 2: r1 = z 5: r2 = x 7: r3 = y

3: if (r1 == 0) 6: y = r2 8: x = r3

4: x = 1

Homework 4

Example 4: Is r1 == r2 == r3 == 1 allowed in the JMM?

Initially, x == y == z == 0

Thread 1 Thread 2 Thread 3 Thread 4

1: z = 1 2: r1 = z 5: r2 = x 7: r3 = y

3: if (r1 == 0) 6: y = r2 8: x = r3

4: x = 1

2

Download