Happens-before、Sequentially consistent

8 篇文章 0 订阅
6 篇文章 0 订阅
Happens-before

粗暴的来说,data race发生在冲突访问没有happens-before 的地方

When a program contains two conflicting accesses (§17.4.1) that are not ordered by a happens-before relationship, it is said to contain a data race.

来源Oracle文档原文

Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second. 两个操作可排序为hb关系,如果一个操作hb另一个,那么第一个对第二个可见且排序在第二个之前

  • 使用hb(x,y)描述x happens-before y
  • 若xy是来自同一线程的操作,且x在程序顺序中在y之前。则hb(x,y)
  • 在一个对象的构造结束段与该对象的finalizer的开始段,有hb边
  • 如果一个操作x后接着一个同步操作synchronized,之后再y 也称hb(x,y)
  • 传递性 If hb(x, y) and hb(y, z), then hb(x, z).
  • Object类(第17.2.1节)的wait方法具有与之关联的锁定和解锁动作; 这些关联的动作定义了它们hb的关系。

应当指出的是,两个动作之间存在先发生关系并不一定意味着在实现中它们必须按照该顺序进行。 如果重新排序产生的结果与合法执行相符,则是合法。
例如,只要没有读取操作发现该事实,就不必在该线程开始之前向线程构造的对象的每个字段写入默认值。
更具体地说,如果两个动作有hb关系,则它们不一定必须按照顺序与没有hb关系的代码构成hb关系。 例如,在一个数据竞争中的一个线程中进行写操作而在另一个线程中进行读操作可能看起来与这些读操作的发生顺序不一致。

happens-before 关系定义了数据竞争发生的地方
A set of synchronization edges, S, is sufficient if it is the minimal set such that the transitive closure of S with the program order determines all of the happens-before edges in the execution. This set is unique.
一组同步边,S,如果S是最小集合,则它是足够的,以使S具有程序顺序执行所有hb 边的传递闭包。 该集合是独一无二的。


以上定义可得:

  • An unlock on a monitor happens-before every subsequent lock on that monitor. hb(unlock,every subsequent lock)

  • A write to a volatile field (§8.3.1.4) happens-before every subsequent read of that field. hb(volatile区域的写, 之后的读)

  • A call to start() on a thread happens-before any actions in the started thread. hb(start() ,线程开始后操作)

  • All actions in a thread happen-before any other thread successfully returns from a join() on that thread.hb(线程操作, 它的join()线程)

  • The default initialization of any object happens-before any other actions (other than default-writes) of a program.hb(对象默认初始化,对象的其他操作)

read r重排序后不在write w前面(没有hb(r,w)),以及没有中间写操作w’ (没有hb(w,w’),hb(w’,r))。
以上情况,如果没有hb顺序去阻止read, read r允许观察到write w
在hb一致的一组操作,hb顺序允许每个read都看到一个write

在这里插入图片描述

Sequentially Consistent

当程序包含两个冲突访问,访问之间没有hb,那么说这里包含了一个数据竞争

When a program contains two conflicting accesses (§17.4.1) that are not ordered by a happens-before relationship, it is said to contain a data race.

当且仅当所有顺序一致的执行都没有数据竞争时,程序才被正确同步。如果一个程序正确同步,则该程序的所有执行将看起来是顺序一致的(第17.4.3节)。

A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.If a program is correctly synchronized, then all executions of the program will appear to be sequentially consistent (§17.4.3).

这是对程序员的极其有力的保证。 程序员无需考虑重新排序即可确定其代码包含数据竞争。 因此,在确定其代码是否正确同步时,无需考虑重新排序。 一旦确定代码已正确同步,程序员就不必担心重新排序会影响代码。

必须正确同步程序,以避免在CPU重新排序代码时可以观察到的违反直觉的行为。 虽然使用正确的同步不能确保程序的整体行为正确。 但是,它的使用确实允许程序员以一种简单的方式来推理程序的可能行为。 正确同步的程序的行为很少依赖于可能的重新排序。 如果没有正确的同步,会产生很奇怪的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值