多线程并将数据传递给新线程

时间:2012-10-19 15:16:27

标签: c# multithreading

我正在编写一个应用程序的问题。它的工作是使用线程解决迷宫问题。一个线程开始,并且对于每个分叉,它在另一个类中调用静态方法,传递另一个线程需要的参数,然后为每个路径启动线程。我的输出都搞砸了,我不确定它是多线程问题还是引用的问题。这是一些代码(每个线程都有一个Explorer类的新实例):

这是每个线程的Run()方法:

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;
    }

这是Move()方法:

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;
}

我的DeepCopy()方法使用的是this问题的接受答案中的方法。我需要提供任何其他信息以便解决问题。在此先感谢您的帮助。

编辑:我的问题主要在于:我在OutOfMemoryException子句中有Thread.Start(),并且线程显示的路径包含损坏的数据(不正确的位置)。我测试了CheckSurroundings()方法,到目前为止只返回正确的位置(迷宫包含在一个二维字符串数组中)。

这是Explorer类的构造函数:

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));
    }

这是创建线程的类:

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.
        }
    }
}

返回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使用传递给子节点的深层副本(通过构造函数)来验证他可以采取的路由。它并不打算改变父母的副本,因为他现在处于迷宫的另一条路径。孩子只需要更新的信息(通过构造函数传递)直到他们“分开”。而且每个孩子必须独立于其他孩子。这就是我想要做的。但我不确定是什么问题,可能是并发问题?请帮忙。如果您还需要其他信息,请告诉我。 -

1 个答案:

答案 0 :(得分:1)

编辑您的更新:

myExplorationMap是原始探索地图的深层副本。您在Explorer构造函数中将位置设置为1,该构造函数更新由所有子线程共享的副本,但它不会更新父线程中的原始MyExplorationMap属性。只有子线程才会知道这个位置被访问过。我假设这是在CheckSurroundings方法中的某个地方使用的?