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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:07:38+00:00 2026-06-18T01:07:38+00:00

[C++ using Visual Studio Professional 2012] Hi All, I am having trouble using std::mutex

  • 0

[C++ using Visual Studio Professional 2012]

Hi All, I am having trouble using std::mutex to prevent main() from changing variables that a second thread is accessing. In the following example (which is a massively simplified representation of my actual program) the function update() runs from the std::thread t2 in main(). update() checks if the vector world.m_grid[i][j].vec is empty and, if it is not, modifies the value contained. main() also accesses and occasionally clears this vector, and as a result if main() clears the vector after the empty check in update() but before world.m_grid[i][j].vec[0] is modified you get a vector subscript out of range error. I am trying to use std::mutex to prevent this from happening by locking barrier before the update() empty check, and releasing it after world.m_grid[i][j].vec[0] has been modified by update(), and after extensive browsing of mutex tutorials and examples I am unable to understand why the following does not have the desired effect:

#include <cstdlib>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;

mutex barrier;

class World
{
public:
    int m_rows;
    int m_columns;
    class Tile
    {
    public:
        vector<int> vec;
        int someVar;
    };

    vector<vector<Tile> > m_grid;
    World (int rows = 100, int columns = 200): m_rows(rows), m_columns(columns), m_grid(rows, vector<Tile>(columns)) {}
};

void update(World& world)
{
    while (true)
    {
        for (int i = 0; i < world.m_rows; ++i)
        {
            for (int j = 0; j < world.m_columns; ++j)
            {
                if (!world.m_grid[i][j].vec.empty())
                {
                    lock_guard<mutex> guard(barrier);
                    world.m_grid[i][j].vec[0] += 5;
                }
            }
        }
    }
}

int main()
{
    World world;
    thread t2(update, ref(world));
    while (true)
    {
        for (int i = 0; i < world.m_rows; ++i)
        {
            for (int j = 0; j < world.m_columns; ++j)
            {
                int random = rand() % 10;
                if (world.m_grid[i][j].vec.empty() && random < 3) world.m_grid[i][j].vec.push_back(1);
                else if (!world.m_grid[i][i].vec.empty() && random < 3) world.m_grid[i][j].vec.clear();
            }
        }  
    } 
t2.join();
return 0;              
}

I must be missing something fundamental here. Ideally the solution would just lock down world.m_grid[i][j] (leaving the rest of world.m_grid accessible to main()), which I assume would involve including a mutex in the class “Tile”, but I run into the same problem as described here: Why does std::mutex create a C2248 when used in a struct with WIndows SOCKET? and have been unable to adapt the solution described to my project, so it would be extra helpful if someone was able to help me out there too.

-Thankyou for your time.

[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-18T01:07:39+00:00Added an answer on June 18, 2026 at 1:07 am

    You need to lock the mutex also in you main function when you access the array:

     ...
     for (int j = 0; j < world.m_columns; ++j) {
          lock_guard<mutex> guard(barrier);
          int random = rand() % 10;
          if (world.m_grid[i][j].vec.empty() && random < 3) world.m_grid[i][j].vec.push_back(1);
          else if (!world.m_grid[i][i].vec.empty() && random < 3) world.m_grid[i][j].vec.clear();
    }
    ...
    

    With mutexes you need to secure all accesses to your data. So far in your code thread 2 generates a mutex when it access the data. However the main thread can change the code as it does not know anything about the mutex. So the main thread can simply change the data.

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

Sidebar

Related Questions

We're using Visual Studio Database Professional and it makes heavy use of SQLCMD variables
I am using visual studio 2012 professional version. I am trying to use Light
Using Visual Studio 2003 Professional under Windows 7 SP1 (64 bit), compiling from the
I just got Visual-Studio 2012 Professional and am using the Entity-Framework (5.0) designer. I've
I am using Visual Studio 2012 Professional, with TFS hosted on a server for
We have developers using Visual Studio 2010 professional and some trying out Visual Studio
I'm using Visual Studio 2008 Professional at work. Recently, I got a new workstation.
We're using Visual Studio 2010 professional to develop and maintain our company ASP (classic
I'm using Visual Studio 2008 Professional in C++ with Windows Form, I have a
Using SQL, SQL server manager 2008, c-sharp.net 4.0 and ms visual studio professional 2010:

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.