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; 
}

 

後記:

這些概念太繞了,感覺外國作者給概念和國內的不一樣,沒有那麼明確。所以,本文只是討論,爭取得出一個結果。但是並不能保證這個結果完全正確。

如果你有新的、正確的發現,歡迎留言討論!

不勝感激!

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