"synchronization actions are totally ordered" 是什么意思?

What does "synchronization actions are totally ordered" mean?

我正在阅读 Java 并发实践,在“16.1.3 Java 内存模型 500 字或更少”中,它说:

The Java Memory Model is specified in terms of actions, which include reads and writes to variables, locks and unlocks of monitors, and starting and joining with threads. The JMM defines a partial ordering called happens-before on all actions within the program. To guarantee that the thread executing action B can see the results of action A (whether or not A and B occur in different threads), there must be a happens-before relationship between A and B. In the absence of a happens-before ordering between two operations, the JVM is free to reorder them as it pleases.

Even though actions are only partially ordered, synchronization actions—lock acquisition and release, and reads and writes of volatile variables—are totally ordered. This makes it sensible to describe happens-before in terms of “subsequent” lock acquisitions and reads of volatile variables.

关于"partial ordering",我找到了this and this,但不太明白"Even though actions are only partially ordered, synchronization actions—lock acquisition and release, and reads and writes of volatile variables—are totally ordered.""synchronization actions are totally ordered"是什么意思?

正在分析语句 "synchronization actions are totally ordered":

  • "synchronization actions"是一组S的程序操作(actions)
  • 我们在集合 S 上有一个关系 R:它是先行关系。也就是说,给定程序语句 abaRb 当且仅当 a 发生在 b.
  • 之前

那么语句的意思就是"relation R is total over S".


"relation R is total over S",意味着对于集合S(和a!=b)中的每两个操作a,b,要么aRb,要么bRa.也就是说,a 发生在 b 之前,或者 b 发生在 a 之前。


如果我们将集合S定义为对同一个锁对象X执行的所有锁获取和锁释放的集合;那么集合 S 完全 由 happens-before 关系排序:假设 aX 的获取由线程 [=35] 执行=],以及 b 由线程 T2 执行的锁获取。然后 a happens-before b (如果 T1 先获取锁。T1 需要先释放锁,然后 T2 才能获取锁);或者 b 发生在 a 之前(以防 T2 先获取锁)。


注意:并非所有关系都是完全的。

例如,关系 <= 是实数的总和。也就是说,对于每对 a,b 个实数,a<=bb<=a 为真。这里的总顺序意味着给定 any 两个项目,我们总是可以决定哪个先出现。给定的关系。

但是关系 P: "is an ancestor of" 并不是所有人类集合的总关系。当然,对于某些人 a,b 来说,aPbab 的祖先)或 bPaba 的祖先)。但是对于他们中的大多数人来说,aPbbPa 都不是真的;也就是说,我们不能使用关系来决定哪个项目出现 "first"(在系谱学术语中)。

回到程序语句,happens-before 关系 R 显然是部分的,在 all 程序语句的集合上(如 "ancestor-of"示例):给定未同步的操作 a,b(不同线程执行的任何操作,在没有适当同步的情况下),aRbbRa 都不成立。