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

  • SEARCH
  • Home
  • 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 9202053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:10:02+00:00 2026-06-17T23:10:02+00:00

my program is based around a world class (essentially world contains a 2D vector

  • 0

my program is based around a “world” class (essentially “world” contains a 2D vector of the child class “tile” that can hold various variables that can move between different tiles, as well as be erased) that is initiated in main(). main() handles the updating of “world” itself, while the function updateDisplay() displays the contents of “world” on the monitor. updateDisplay() is run by a separate C++11 thread (that is initiated in main()), and accesses “world” through a passed reference.

My problem is that I am encountering a rare crash that occurs because, in the (simplified) code excerpt from updateDisplay() below, the vector world.m_grid[i][j].m_rabbit is occasionally cleared by main() after the “if (!world.m_grid[i][j].m_rabbit.empty())” check in updateDisplay(), but before the subsequent accessing of the m_rabbit[0] vector member, resulting in a “vector subscript out of range” error.

for (int i = 0; i < rows; ++i)
{
    for (int j = 0; j < columns; ++j)
    {

        if (!world.m_grid[i][j].m_rabbit.empty())
        {
            s_rabbit_64.setScale(absTileScale * world.m_grid[i][j].m_rabbit[0].m_size, absTileScale * world.m_grid[i][j].m_rabbit[0].m_size);
            s_rabbit_64.setPosition(x * k + (12 * absTileScale), y * (i - winFocus.y) + (12 * absTileScale));
            window.draw(s_rabbit_64);
        }
    }
}

My question is, how do I go about locking down world.m_grid[i][j].m_rabbit vector so that main() cannot alter it while the updateDisplay() thread is accessing it? I gather this is achieved through use of std::mutex, but I have been unable to find a guide that gives me a good enough understanding of this to apply it to my program. I hope this is clear enough to understand (the program is much larger than this excerpt and I didn’t want to do a massive text-dump), please ask for clarification on any points you need.

Thankyou for your attention.

[edit] spelling

  • 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-17T23:10:03+00:00Added an answer on June 17, 2026 at 11:10 pm

    Welcome to the wild and wooly world of multithreading. Take a seat; you’ll be here for a long time. As in, the rest of your programming career kind of long. It’s scary, full of treachery and subtle bugs, and also pays really well if you can do it right 🙂

    You’ve done a pretty good job of identifying the specific bug in this case, actually. What you’ll want is to have a mutex guarding all accesses and mutations of state that’s shared between threads. In your case: lock the mutex in your main thread before clearing the vector, and unlock the mutex after you’re done. Similarly, lock the mutex in your processing thread before accessing it, and unlock it when you’re done. In this particular case you’ll probably end up locking the entire double-for loop you’ve got going there.

    This answer is a pretty good explanation of what the mutex is doing.


    Yes, there are faster and more elegant ways of doing things, but let’s stick with correctness for now.

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

Sidebar

Related Questions

I develop a Python-based drawing program, Whyteboard . I have tools that the user
I have an OLD mac C/C++ program that I'm maintaining. It's still based on
I'm writing a cross-platform Qt-based program that from time to time needs to play
I have created a Spherical Panoramas in Photoshop that I can drag around and
I'm trying to take up node.js for a comet-based program. Essentially I'm going to
Can anyone give a concise set of real-world considerations that would drive the choice
Making a small WCF test program which is based on a Store that has
I'm making a baseball program based on the strategy games produced by Avalon Hills
I m making an rmi client server based program which is suppose to pass
I am trying to learn a java-based program, but I am pretty new to

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.