Docsity

Data Race Example - Lecture Slides | CMSC 433, Study notes of Programming Languages

Partial preview of the text

Download Data Race Example - Lecture Slides | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

Data Race Example

public class Example extends Thread { private static int cnt = 0; // shared state public void run() { int y = cnt; cnt = y + 1; } public static void main(String args[]) { Thread t1 = new Example(); Thread t2 = new Example(); t1.start(); t2.start(); } }

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 0 Start: both threads ready to run. Each will increment the global count. Shared state

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 0 T1 is pre-empted. T executes, grabbing the global counter value into y. Shared state y = 0 y = 0

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 1 T2 executes, storing the incremented cnt value. Shared state y = 0 y = 0

But When I Run it Again?

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 0 Start: both threads ready to run. Each will increment the global count. Shared state

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 1 T1 executes again, storing the counter value Shared state y = 0

Data Race Example

static int cnt = 0; t1.run() { int y = cnt; cnt = y + 1; } t2.run() { int y = cnt; cnt = y + 1; } cnt = 1 T1 finishes. T2 executes, grabbing the global counter value into y. Shared state y = 0 y = 1

What Happened?

  • In the first example, t1 was preempted after it read

the counter but before it stored the new value.

  • Depends on the idea of an atomic action
  • Violated an object invariant
  • A particular way in which the execution of two

threads is interleaved is called a schedule. We

want to prevent this undesirable schedule.

  • Undesirable schedules can be hard to reproduce,

and so hard to debug.

Question

• If instead of

int y = cnt; cnt = y+1;

• We had written

  • cnt++;

• Would the result be any different?

• Answer: NO!

  • Don’t depend on your intuition about atomicity

What can this example generate?

static int cnt = 2; t1.run() { int y = cnt; cnt = y * 2; } t2.run() { int y = cnt; cnt = y + 1; }

Avoiding Interference: Synchronization public class Example extends Thread { private static int cnt = 0; static Object lock = new Object(); public void run() { synchronized (lock) { int y = cnt; cnt = y + 1; } } … } Lock , for protecting the shared state Acquires the lock; Only succeeds if not held by another thread Releases the lock

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 0 T1 reads cnt into y y = 0

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 0 T1 is pre-empted. T2 attempts to acquire the lock but fails because it’s held by T1, so it blocks y = 0