Reputation: 1212
I have some issues with an application I'm coding. It's job is to solve a maze using threads. One thread begins, and for each bifurcation it calls a static method in another class, passing parameters that the other thread needs, and then starts threads for each path. My outputs are all messed up, and I'm not sure if it's a Multithreading problem or maybe a problem with the references. Here's some of the code (Each thread is given a new instance of an Explorer
class):
This is the Run()
method for each thread:
public void Explore()
{
while (ImDone == false)
{
Move();
if (ActualPosition[0] != Labyrinth.ExitPoint[0] ||
ActualPosition[1] != Labyrinth.ExitPoint[1]) //I'm not at the end..
continue;
PrintMyStatus(); //Print in the console my parents and my complete route..
break;
}
This is the Move()
method:
List<int[]> validPaths = CheckSurroundings(); //returns a list of paths
switch (validPaths.Count)
{
case 0:
ImDone = true; //No more routes available
break;
case 1:
MarkMyPosition(validPaths); //Change internal variables to the only new path
break;
default:
lock(this) //As this involves thread creating I locked it..
{
//Creating deep copies of my "History" so my childs can have them..
List<int[]> DCValidPaths = DeepCopy(validPaths);
List<string> DCMyParents = DeepCopy(MyParents);
string[,] DCMyExplorationMap = DeepCopy(MyExplorationMap);
List<int[]> DCMyPositions = DeepCopy(MyPositions);
foreach (var path in validPaths)
{
DCMyParents.Add(Thread.CurrentThread.Name); //Adding myself as a parent
ExplorationManager.ExplorerMaker(
DCValidPaths,
DCMyParents,
DCMyExplorationMap,
DCMyPositions,
ID); //Threads are created in the static class
}
}
break;
}
My DeepCopy()
method is using is the one in the accepted answer of this question. Any other info needed I'll be more than happy to provide in order to solve the problem. Thanks in advance for your help.
EDITS: My problem consists basically in: I'm having an OutOfMemoryException
in the Thread.Start()
clause and the Path displayed by the threads contain corrupted data (incorrect positions). I tested the CheckSurroundings()
method and by far only returns correct positions (The labyrinth is contained in a two-dimensional string array).
This is the constructor of the Explorer
class:
public Explorer(List<string> myParents, int[] actualPosition, string[,] myExplorationMap,
List<int[]> myPositions, int ID)
{
//Here I pass the data specified in Move();
MyParents = myParents;
ActualPosition = actualPosition;
MyExplorationMap = myExplorationMap;
MyPositions = myPositions;
this.ID = ID + 1; //An ID for reference
//Marking position in my map with "1" so I know I've been here already,
//and adding the position given to my list of positions
MyExplorationMap[ActualPosition[0], ActualPosition[1]] = "1";
MyPositions.Add(DeepCopy(ActualPosition));
}
And this is the class creating the threads:
public static class ExplorationManager
{
public static void ExplorerMaker(List<int[]> validPaths, List<string> myParents, string[,] myExplorationMap, List<int[]> myPositions, int ID)
{
foreach (var thread in validPaths.Select
(path => new Explorer(myParents, path, myExplorationMap, myPositions,ID)).
Select(explorer => new Thread(explorer.Explore)))
{
thread.Name = "Thread of " + ID + " generation";
thread.Start(); //For each Path in Valid paths, create a new instance of Explorer and assign a thread to it.
}
}
}
And the method returning the ValidPaths
private List<int[]> CheckSurroundings()
{
var validPaths = new List<int[]>();
var posX = ActualPosition[0];
var posY = ActualPosition[1];
for (var dx = -1; dx <= 1; dx++)
{
if (dx == 0 || (posX + dx) < 0 || (posX + dx) >= Labyrinth.Size ||
MyExplorationMap[posX + dx, posY] == "1") continue;
var tempPos = new int[2];
tempPos[0] = posX + dx;
tempPos[1] = posY;
validPaths.Add(tempPos);
}
for (var dy = -1; dy <= 1; dy++)
{
if (dy == 0 || (posY + dy) < 0 || (posY + dy) >= Labyrinth.Size ||
MyExplorationMap[posX, posY + dy] == "1") continue;
var tempPos = new int[2];
tempPos[0] = posX;
tempPos[1] = posY + dy;
validPaths.Add(tempPos);
}
//This method checks up, down, left, right and returns the posible routes as `int[]` for each one
return validPaths;
}
CheckSurroundings uses the deep copy passed to the child (via the constructor) to validate the routes he can take. It is NOT intended to alterate the parent's copy, because he is now in ANOTHER path of the labyrinth. The child only needs the information updated (passed via the constructor) UNTIL the point they "separate". And also EACH child must be independent from the other childs. That's what I'm trying to do. But I'm not sure what's wrong, maybe concurrency problem? Please help. If you need anything else let me know. –
Upvotes: 0
Views: 294
Reputation: 3302
EDIT for your updates:
myExplorationMap is a deep copy of the original exploration map. You set the position to 1 in the Explorer constructor, which updates the copy that is shared by all of the child threads, but it does not update the original MyExplorationMap property in the parent thread. Only the child threads will know this position was visited. I assume this is used somewhere in the CheckSurroundings method?
Upvotes: 1