Concurrency - Thread Interference (Interleave on shared data)

1 - About

Same name than Concurrency - Race Condition (Concurrency Problem) ?

how errors are introduced when multiple threads access shared data.

Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

Resolved by Concurrency - Synchronization

Because they are unpredictable, thread interference bugs can be difficult to detect and fix.

3 - Interleave Counter Example

  • A counter c with two single expression increment and decrement.

public void increment() {
        c++;  // c-- for decrement
}

  • This single expression translate to multiple steps by the virtual machine

Retrieve the current value of c.
Increment the retrieved value by 1.
Store the incremented value back in c.

  • Example of a particular interleaving where Thread A data operation is lost

Thread A: Retrieve c.
Thread B: Retrieve c.
Thread A: Increment retrieved value; result is 1.
Thread B: Decrement retrieved value; result is -1.
Thread A: Store result in c; c is now 1.
Thread B: Store result in c; c is now -1.
Thread A's result is lost, overwritten by Thread B. 

This particular interleaving is only one possibility. Under different circumstances it might be Thread B's result that gets lost, or there could be no error at all.

4 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap