メソッドスレッドの安全性を呼び出して実行:パラレル以下を持つ
List<Person> persons = // list of persons
Parallel.ForEach(persons, (i) => {
AddAge(i);
});
// Does this method needs to be thread safe?
// Why?
public void AddAge(Person person)
{
// Multiple threads execute here at once. However they're
// working with their own "person" object, therefore
// each thread won't corrupt others "person" object - is this assumption correct?
person.Age =+ 10;
}
- 一人一人が自分の別のスレッドで「別に」更新され、もう1つは別とは何の関係もありませんので、ないAddAge()メソッドスレッドセーフでなければならない?
- CLRはスレッドごとに「AddAge()」という独自のコピーを実行してスレッド間で分離しますか?
「参照渡し」は、その行の参照で何も渡していません。また、あなたの 'AddAge'は数字を計算し、それを床に落とします。それは実際には(コンパイルされません)作成した番号で何もしません。 – Servy
Personは参照型です。 AddAgeに到達すると、そのオブジェクトへの参照が送信されます。それはありませんか? – BobSwanson
変数の値は参照値です。その変数は、参照ではなく値によって渡されます。 – Servy