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 8680971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:23:05+00:00 2026-06-12T21:23:05+00:00

I have a multidimensional array with mutexes, and I use them so some critical

  • 0

I have a multidimensional array with mutexes, and I use them so some critical sections. At the end is sketch of my class.

Now, I tried to add and delete characters in different threads without stopping, and at the same time getting characters from another thread to test if my server will work properly, but I get an exception when I try to get a character that says something like this: “Collection was modified during foreach..”

Anyone knows why? I have read the documentation of the lock statement in MSDN and it should work, I don’t know what I am doing wrong. Maybe new object[] can’t be used as a mutex?

If lock(objectReference) shouldn’t be used in this case, what should I use instead?

Sketch of my class:

partial class MapCharacter
{
    public MapCharacter()
    {
        mutexes = new object[5, 5];
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                mutexes[x, y] = new object();
            }
        }
    }

    object[,] mutexes;

    public object RequestMutex(int x, int y)
    {
        return mutexes[x, y];
    }

    public void RemoveCharacter(Character characterToRemove)
    {
        lock (RequestMutex(characterToRemove.X, characterToRemove.Y))
        {
            loggedCharacters.Remove(characterToRemove);
        }
    }

    public void AddCharacter(Character characterToAdd)
    {
        lock (RequestMutex(characterToRemove.X, characterToRemove.Y))
        {
            loggedCharacters.Add(characterToAdd);
        }
    }

    public Character[] GetCharacters(int x, int y)
    {
        lock (RequestMutex(characterToRemove.X, characterToRemove.Y))
        {
            foreach (var charact in loggedCharacters)
            {
                // exception here
            }
        }
    }

    // more methods that use the mutex when trying to remove/add/modify characters
}
  • 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-12T21:23:07+00:00Added an answer on June 12, 2026 at 9:23 pm

    Your problem is that you are locking the individual characters but not loggedCharacters. You need to protect loggedCharacters with a lock or make it a concurrent collection in order to avoid that exception.

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

Sidebar

Related Questions

I have a multidimensional array, and I would have multiple arrays within it. Some
I have a multidimensional array in python like: arr = [['foo', 1], ['bar',2]] Now,
I have a multidimensional array or arrays which I also use in my configuration
I have a multidimensional array in which some values are present i want to
I have a multidimensional array. And some of array's elements have 'inactive' flag. I
I have a multidimensional array and am trying to group them according to the
I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now
ok i now it supose to be simple y have a multidimensional array, I
hi i have multidimensional array in php i want to remove an index from
I have a multidimensional array which I am looping through using a foreach loop.

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.