Concurrency - (Shared) Memory Inconsistency

Data System Architecture

About

Errors that result from inconsistent views of shared memory.

Memory consistency errors occur when different threads have inconsistent views of what should be the same data.

The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation.

The key to avoiding memory consistency errors is understanding the happens-before relationship.

Example

Suppose:

  • A simple int field is defined and initialized:
int counter = 0;
  • The counter field is shared between two threads, A and B.
  • Thread A increments counter:
counter++;
  • Then, shortly afterwards, thread B prints out counter:
System.out.println(counter);

If the two statements had been executed in the same thread, it would be safe to assume that the value printed out would be “1”. But if the two statements are executed in separate threads, the value printed out might well be “0”, because there's no guarantee that thread A's change to counter will be visible to thread B — unless the programmer has established a happens-before relationship between these two statements. (ie between increment and println)





Discover More
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 - Happens Before relationship

The happens-before relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement. The results of a write by one thread are guaranteed to be...
Java Conceptuel Diagram
Java Concurrency - (Concurrent) Collections

All of the concurrent collections help avoid Memory Consistency Errors by defining a happens-before relationship between: an operation that adds an object to the collection with subsequent operations...
Java Conceptuel Diagram
Java Concurrency - Happens-before Relationship

in Java The following actions create happens-before relationships: synchronized construct. When a thread releases an intrinsic lock, a happens-before relationship is established between that action...
Data System Architecture
Parallel Programming - (Function|Operation)

A function or an operator has to be: commutative and associative in order to be computed correctly in parallel. A properly constructed operation is inherently parallelism, so long as the function(s)...
Process States
Process - Shared State (Shared Data)

Shared state between processes Operations upon shared states are critical sectioncritical sections that must be mutually exclusive. Failure to obey this rule opens up the possibility of corrupting the...
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