静的変数(単純なint)を含むこの静的クラスがあります。lock()
スレッドのメソッドにを実装したRun()
ので、他のスレッドがこのクラスに同時にアクセスすることはできませんが、変数はまだ狂っており、重複、めちゃくちゃ高い値などが表示されます。
これはクラスです:
public static class ExplorationManager
{
public static int Counter = 0;
public static void ExplorerMaker(List<int[]> validPaths, List<string> myParents, string[,] myExplorationMap, List<int[]> myPositions)
{
foreach (var thread in validPaths.Select
(path => new Explorer(myParents, path, myExplorationMap, myPositions)).
Select(explorer => new Thread(explorer.Explore)))
{
thread.Name = "Thread of " + Counter + " generation";
Counter++;
thread.Start();
}
}
}
この変数を「もっと」スレッドセーフにする方法はありますか?