mox692 のブログ

妄想の書き留め場所.

rustのthread::spawn

rustのthread生成関数、std::thread::spawn()にて、docコメントに以下の記述があった.

/// In terms of [atomic memory orderings],  the completion of the associated
/// thread synchronizes with this function returning. In other words, all
/// operations performed by that thread are ordered before all
/// operations that happen after `join` returns.  

アトミックメモリの順序に関しては、関連付けられたスレッドの完了は、この関数が戻るのと同期します。 つまり、そのスレッドによって実行されるすべての操作は、joinが戻った後に発生するすべての操作の前に順序付けられます。

おおおこの感じ、javaのhappens-before制約やん!ってなった.

つまり、

let j1 = thread::spawn(|| {
    // something...
})
j1.join().expect("fail1")  
  
let j2 = thread::spawn(|| {
    // something...
})
j2.join().expect("fail2")

と書けば、j1 -> j2 というorderingは確約される、ってことかな.

/* -----codeの行番号----- */