@@ -222,9 +222,9 @@ shared mutable state がとてもとても悪いものであるということ
222
222
ポインタの誤った使用の防止には [ 所有権のシステム] ( ownership.html ) が役立ちますが、このシステムはデータ競合を排除する際にも同様に一役買います。
223
223
データ競合は、並行性のバグの中で最悪なものの一つです。
224
224
225
- <!-- As an example, here is a Rust program that would have a data race in many -->
225
+ <!-- As an example, here is a Rust program that could have a data race in many -->
226
226
<!-- languages. It will not compile: -->
227
- 例として、多くの言語で起こるようなデータ競合を含んだRustプログラムをあげます 。
227
+ 例として、多くの言語で起こりうるようなデータ競合を含んだRustプログラムをあげます 。
228
228
これは、コンパイルが通りません。
229
229
230
230
``` ignore
@@ -254,10 +254,27 @@ fn main() {
254
254
```
255
255
256
256
<!-- Rust knows this wouldn't be safe! If we had a reference to `data` in each -->
257
- <!-- thread, and the thread takes ownership of the reference, we'd have three -->
258
- <!-- owners! -->
257
+ <!-- thread, and the thread takes ownership of the reference, we'd have three owners! -->
258
+ <!-- `data` gets moved out of `main` in the first call to `spawn()`, so subsequent -->
259
+ <!-- calls in the loop cannot use this variable. -->
259
260
Rustはこれが安全でないだろうと知っているのです!
260
261
もし、各スレッドに ` data ` への参照があり、スレッドごとにその参照の所有権があるとしたら、3人の所有者がいることになってしまうのです!
262
+ ` data ` は最初の ` spawn ` の呼び出しで ` main ` からムーブしてしまっているので、ループ内の続く呼び出しはこの変数を使えないのです。
263
+
264
+ <!-- Note that this specific example will not cause a data race since different array -->
265
+ <!-- indices are being accessed. But this can't be determined at compile time, and in -->
266
+ <!-- a similar situation where `i` is a constant or is random, you would have a data -->
267
+ <!-- race. -->
268
+ この例では配列の異ったインデックスにアクセスしているのでデータ競合は起きません。
269
+ しかしこの分離性はコンパイル時に決定出来ませんし ` i ` が定数や乱数だった時にデータ競合が起きます。
270
+
271
+ <!-- So, we need some type that lets us have more than one owning reference to a -->
272
+ <!-- value. Usually, we'd use `Rc<T>` for this, which is a reference counted type -->
273
+ <!-- that provides shared ownership. It has some runtime bookkeeping that keeps track -->
274
+ <!-- of the number of references to it, hence the "reference count" part of its name. -->
275
+ そのため、1つの値に対して2つ以上の所有権を持った参照を持てるような型が必要です。
276
+ 通常、この用途には ` Rc<T> ` を使います。これは所有権の共有を提供するリファレンスカウントの型です。
277
+ 実行時のなんちゃらがひつようで、リファレンスの数をカウントします。なのでリファレンスカウントです。
261
278
262
279
<!-- So, we need some type that lets us have more than one reference to a value and -->
263
280
<!-- that we can share between threads, that is it must implement `Sync`. -->
0 commit comments