e-tipsmemo.hatenablog.com
このような記事を書いたが、そもそもthreadをたくさんjoinした後に、
その後の処理はそれらスレッドの終了を待っているのだろうか。と思ったので確認してみた。
#[cfg(test)] mod tests { use super::*; fn hoge(thread: u32, num: u32) -> u32 { for x in 0..num { println!("th:{}, count:{}", thread, x); thread::sleep(std::time::Duration::from_millis(1000)); } thread } #[test] fn thread_barrer() { let v = vec![20, 30, 10, 15, 17]; let mut children = vec![]; for t in 0..v.len() { let num = v[t as usize]; children.push(thread::spawn(move || hoge(t as u32, num))); } let mut ret = vec![]; for child in children { ret.push(child.join().unwrap()); } println!("{:?}", ret.iter().fold(0, |s, &t| s + t)); } }
結果としてこのプログラムは全てのchild threadがおわってから次の処理を行うという意図したものになってた。
$ cargo test
Compiling multi v0.1.0 (*** )
Finished dev [unoptimized + debuginfo] target(s) in 1.3 secs
Running target/debug/deps/multi-1bf733f9a047a566
running 1 test
th:2, count:0
(略)
th:2, count:3
th:1, count:3
th:3, count:3
th:0, count:3
th:4, count:3
th:1, count:4
th:4, count:4
th:2, count:4
th:3, count:4
th:0, count:4
th:0, count:5
th:3, count:5
th:4, count:5
th:2, count:5
th:1, count:5
(略)
th:0, count:12
th:1, count:12
th:3, count:12
th:4, count:12
th:0, count:13
th:4, count:13
th:3, count:13
th:1, count:13
th:0, count:14
(略)
th:1, count:20
th:1, count:21
th:1, count:22
th:1, count:23
th:1, count:24
th:1, count:25
th:1, count:26
th:1, count:27
th:1, count:28
th:1, count:29
10
.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measuredCorrect way to wait until all threads are finished
結論:勝手に待ってくれている。

並行コンピューティング技法 ―実践マルチコア/マルチスレッドプログラミング
- 作者: Clay Breshears,千住治郎
- 出版社/メーカー: オライリージャパン
- 発売日: 2009/12/21
- メディア: 大型本
- 購入: 12人 クリック: 598回
- この商品を含むブログ (38件) を見る

Mastering Rust: Advanced concurrency, macros, and safe database
- 作者: Vesa Kaihlavirta
- 出版社/メーカー: Packt Publishing
- 発売日: 2017/05/30
- メディア: Kindle版
- この商品を含むブログを見る