program order

参考资料:

  1. JSL:https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4
  2. perfbook why memory barriers:https://blog.csdn.net/reliveIT/article/details/105902477

 

一. 什么是intra-thread semantics

引用自:https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4

The memory model determines what values can be read at every point in the program. The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that the values seen by each read are determined by the memory model. When we refer to this, we say that the program obeys intra-thread semantics. Intra-thread semantics are the semantics for single-threaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. To determine if the actions of thread t in an execution are legal, we simply evaluate the implementation of thread t as it would be performed in a single-threaded context, as defined in the rest of this specification.

  • The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that the values seen by each read are determined by the memory model:每个独立线程的操作必须由线程语义控制,每个操作能看到的值由内存模型决定(这里我联系不了上下文,有疑惑,写这段的目的是什么?)
  • Intra-thread semantics are the semantics for single-threaded programs:Intra-thread semantics就是单线程程序语义
    • 什么是单线程程序语义?the semantics for single-threaded programs:我没有找到比较官方的解释,个人认为就是单线程环境的代码的实际执行顺序,他是允许没有相关性的操作之间乱序的,因为单线程环境下这不会导致乱序结果。换句话来说,the semantics for single-threaded programs允许那些在单线程内不会改变单线程程序执行结果的重排序
  • And Intra-thread semantics allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread:Intra-thread semantics允许线程内基于读操作看到的值的行为完全可预测
  • Intra-thread semantics are the semantics for single-threaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. :注意这个and,and说明了Intra-thread semantics不仅是单线程程序语义,并且还允许读操作看到值的行为完全可预测,所以说Intra-thread semantics是单线程程序语义的扩展,还包含了值的可见性描述

结论:

  • Intra-thread semantics是the semantics for single-threaded programs的扩展。
  • Intra-thread semantics遵循the semantics for single-threaded programs单线程程序语义,并且描述了线程内的读写操作的完全可预测,即线程内写操作的值对线程内读操作立即可见,没有二义性。
    • 例如读操作如果可能读到新值,也可能读到旧值,那么这个行为就属于不可预测,存在二义性
    • 如果保证可见性,那么线程内读操作都能读到写的最新的值,那行为预测就很明确

 

二. 什么是Sequential consistency

引用自:https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.3

Sequential consistency is a very strong guarantee that is made about visibility and ordering in an execution of a program. Within a sequentially consistent execution, there is a total order over all individual actions (such as reads and writes) which is consistent with the order of the program, and each individual action is atomic and is immediately visible to every thread.

  • 顺序一致性对可见性和程序执行顺序有非常强的保证:Sequential consistency is a very strong guarantee that is made about visibility and ordering in an execution of a program.
  • 在顺序一致性的执行中,所有单个操作(如读、写)都有一个总的顺序,这个总的顺序和程序顺序一致:Within a sequentially consistent execution, there is a total order over all individual actions (such as reads and writes) which is consistent with the order of the program
  • 单操作是原子并且每个线程立即可见,也就是说读写操作都是原子的,写对每个线程可见,读能读到最新的值:each individual action is atomic and is immediately visible to every thread.

结论:

  • Sequential consistency是一个强一致的模型,顺序一致性的执行顺序就是代码顺序,并且读写操作原子且立即可见

 

三. 什么是program order

引用自:https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.3

Among all the inter-thread actions performed by each thread t, the program order of t is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of t.

A set of actions is sequentially consistent if all actions occur in a total order (the execution order) that is consistent with program order, and furthermore, each read r of a variable v sees the value written by the write w to v such that:

1.w comes before r in the execution order, and

2.there is no other write w' such that w comes before w' and w' comes before r in the execution order

要特别注意的两个单词:

  1. inter-thread:线程间
  2. intra-thread:线程内
  • 在线程t执行的所有线程间操作中,t的program order是一个总顺序,这个总的顺序反映了根据线程t的intra-thread semantics执行这些操作的顺序:Among all the inter-thread actions performed by each thread t, the program order of t is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of t.
    • program order反映了intra-thread semantics中的执行顺序(reflect,而不是consistent)
  • A set of actions is sequentially consistent if all actions occur in a total order (the execution order) that is consistent with program order, and furthermore, each read r of a variable v sees the value written by the write w to v such that: 1.w comes before r in the execution order, and 2.there is no other write w' such that w comes before w' and w' comes before r in the execution order
    • 一组操作如果满足以下两个条件,那么认为这组操作是sequentially consistent
      • 这组操作中所有操作的执行顺序和program order一致
      • 并且满足可见性,即如果写w在读r之前执行,那么r就能读到w写的值v
    • sequentially consistent是program order的扩展,不仅要求两者的执行顺序一致,还描绘了操作间的可见性
  • 上一节的sequentially consistent:Within a sequentially consistent execution, there is a total order over all individual actions (such as reads and writes) which is consistent with the order of the program, and each individual action is atomic and is immediately visible to every thread.
    • program order和sequentially consistent的执行顺序一致,sequentially consistent的执行顺序和程序顺序the order of the program一致,所以我认为program order也就是the order of the program

结论

  1. the semantics for single-threaded programs
  2. Intra-thread semantics是the semantics for single-threaded programs的扩展
  3. program order反映了Intra-thread semantics中的执行顺序(是reflects the order,而不是consistent with the execution order)
  4. Sequential consistency是program order的扩展,并且Sequential consistency和program order的执行顺序是一致的(这里是consistent with the execution order)
  5. Sequential consistency的执行顺序和the order of the program是一致的
  6. 所以,可以推出program order就是the order of the program

 

四. perfbook why memory barriers中program order就是代码顺序的例子

《perfbook why memory barriers》:https://blog.csdn.net/reliveIT/article/details/105902477

由perfbook中的例子来看,program order就是程序顺序,而不是单线程代码的实际的执行顺序。如果program order允许合理的重排序(即允许没有相关性的程序乱序),例如4.3中foo函数,写a和写b是没有数据相关性的(写后写是对同一个变量,这里是两个变量),如果写a和写b的顺序调过来,那么4.3中该例将不能说明store buffer和memory barriers

void foo(void) { 
    a = 1; 
    b = 1; 
}

 

后记:

这些概念太绕了,感觉外国作者给概念和国内的不一样,没有那么明确。所以,本文只是讨论,争取得出一个结果。但是并不能保证这个结果完全正确。

如果你有新的、正确的发现,欢迎留言讨论!

不胜感激!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章