Happened-before

In computer science, the happened-before relation (denoted: [math]\displaystyle{ \to \; }[/math]) is a relation between the result of two events, such that if one event should happen before another event, the result must reflect that, even if those events are in reality executed out of order (usually to optimize program flow). This involves ordering events based on the potential causal relationship of pairs of events in a concurrent system, especially asynchronous distributed systems. It was formulated by Leslie Lamport.[1] The happened-before relation is formally defined as the least strict partial order on events such that:

* If events [math]\displaystyle{ a \; }[/math] and [math]\displaystyle{ b \; }[/math] occur on the same process, [math]\displaystyle{ a \to b\; }[/math] if the occurrence of event [math]\displaystyle{ a \; }[/math] preceded the occurrence of event [math]\displaystyle{ b \; }[/math].
* If event [math]\displaystyle{ a \; }[/math] is the sending of a message and event [math]\displaystyle{ b \; }[/math] is the reception of the message sent in event [math]\displaystyle{ a \; }[/math], [math]\displaystyle{ a \to b\; }[/math].

If two events happen in different isolated processes (that do not exchange messages directly or indirectly via third-party processes), then the two processes are said to be concurrent, that is neither [math]\displaystyle{ a \to b }[/math] nor [math]\displaystyle{ b \to a }[/math] is true.[2]

If there are other causal relationships between events in a given system, such as between the creation of a process and its first event, these relationships are also added to the definition. For example, in some programming languages such as Java,[3] C, C++ or Rust, a happens-before edge exists if memory written to by statement A is visible to statement B, that is, if statement A completes its write before statement B starts its read.

Like all strict partial orders, the happened-before relation is transitive, irreflexive (and vacuously, asymmetric), i.e.:

* [math]\displaystyle{ \forall a, b, c }[/math], if [math]\displaystyle{ a \to b\; }[/math] and [math]\displaystyle{ b \to c\; }[/math], then [math]\displaystyle{ a \to c\; }[/math] (transitivity). This means that for any three events [math]\displaystyle{ a, b, c }[/math], if [math]\displaystyle{ a }[/math] happened before [math]\displaystyle{ b }[/math], and [math]\displaystyle{ b }[/math] happened before [math]\displaystyle{ c }[/math], then [math]\displaystyle{ a }[/math] must have happened before [math]\displaystyle{ c }[/math].
* [math]\displaystyle{ \forall a, a \nrightarrow a }[/math] (irreflexivity). This means that no event can happen before itself.
* [math]\displaystyle{ \forall a, b, }[/math] if [math]\displaystyle{ a \to b }[/math] then [math]\displaystyle{ b \nrightarrow a }[/math] (asymmetry). This means that for any two events [math]\displaystyle{ a, b }[/math], if [math]\displaystyle{ a }[/math] happened before [math]\displaystyle{ b }[/math] then [math]\displaystyle{ b }[/math] cannot have happened before [math]\displaystyle{ a }[/math].

Let us observe that the asymmetry property directly follows from the previous properties: by contradiction, let us suppose that [math]\displaystyle{ \forall a, b, }[/math] we have [math]\displaystyle{ a \to b\; }[/math] and [math]\displaystyle{ b \to a }[/math]. Then by transitivity we have [math]\displaystyle{ a \to a, }[/math] which contradicts irreflexivity.

The processes that make up a distributed system have no knowledge of the happened-before relation unless they use a logical clock, like a Lamport clock or a vector clock. This allows one to design algorithms for mutual exclusion, and tasks like debugging or optimising distributed systems.

## See also

* Race condition
* Java Memory Model
* Lamport timestamps
* Logical clock

## Citations

1. ↑ Lamport, Leslie (1978). "Time, Clocks and the Ordering of Events in a Distributed System", Communications of the ACM, 21(7), 558-565.
2. ↑ "Distributed Systems 3rd edition (2017)" (in en-US). https://www.distributed-systems.net/index.php/books/ds3/.
3. ↑ Goetz et al. 2006, pp. 339-342, §16.1.3 The Java Memory Model in 500 words or less.

## References

* Goetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). Java Concurrency in Practice. Addison Wesley. ISBN 0-321-34960-1. https://archive.org/details/javaconcurrencyi00goet.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Happened-before. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Happened-before&oldid=3368820"