Concurrency - Thread Interference (Interleave on shared data)

Data System Architecture

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. One solution is to make the data immutable

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.

Documentation / Reference





Discover More
Data System Architecture
Concurrency - Race Condition (Concurrency Problem)

A Race condition is the only concurrent problem that can happen when two threads manipulate the same state (value) in the same time-lapse, the last thread to write the state will overwrite the state modification...
Data System Architecture
Concurrency - Thread Safe

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread...
Data System Architecture
Data Concurrency - Atomic Variable Access

Atomic operations (in order to get a value) cannot be interleaved, so they can be used without fear of thread interference. Primitive atomic instructions on a CPU instruction level: Test-and-set ...
Card Puncher Data Processing
Go - Goroutine (concurrent function execution)

A goroutine is a concurrent function execution. A channel is a communication mechanism that allows one goroutine to pass values of a specified type to another goroutine. The function main runs in a goroutine...
Java Conceptuel Diagram
Java Concurrency - Deadlock

deadlock in Java. The Java programming language neither prevents nor requires detection of deadlock conditions. Programs where threads hold (directly or indirectly) locks on multiple objects should...
Java Conceptuel Diagram
Java Concurrency - Fine-grained synchronization

Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose: class MsLunch has two instance fields, c1 and c2, c1 and c2 are never used together....
Java Conceptuel Diagram
Java Concurrency - Synchronized method

Synchronized method To make a method synchronized, simply add the synchronized keyword to its declaration: A synchronized method: performs a lock action when it is invoked; its body is not executed...
Card Puncher Data Processing
Software Development - (Stateless|Stateful)

Stateless or state-full refers to the fact that a unit of program (process, function, procedure) have a state or not (Ie variable that may change). stateless Parallel aggregate operations over...
Data System Architecture
State - Immutable

A state who is immutable cannot be changed. An variable (object) is considered immutable if its state cannot change after it is constructed. Immutable objects are particularly useful in concurrent applications....
Process States
Thread - Shared Variable

shared state in threads. Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of...



Share this page:
Follow us:
Task Runner