Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:59:47+00:00 2026-06-13T02:59:47+00:00

I have some issues with an application I’m coding. It’s job is to solve

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:59:48+00:00Added an answer on June 13, 2026 at 2:59 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some bad performance issues in my application. One of the big operations
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I'm using Spring Tool Suite 3.1 and I have some issues deploying a web
I try to build my first Facebook application and I have some issues. I
I am using Symfony 1.2 and I have some issues switching context. This code
I have been having some issues retrieving JSON data from a WCF service application
I have some issues when I'm trying to deploy my application in windows azure.
I have some issues with the Riched20.dll file which is used by my application,
I have an application deployed into multiple zones and there are some issues with
In using our TeamCity Continuous Integration server we have uncovered some issues that we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.