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

The Archive Base Latest Questions

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

Lets say I have a 2D array and this function: void addVec(std::vector<std::vector<short> >& arr,

  • 0

Lets say I have a 2D array and this function:

void addVec(std::vector<std::vector<short> >& arr, size_t idx){ 
   arr[idx].push_back(idx);
}

std::vector<std::vector<short> > arr(2);
boost::threads th1(addVec, boost::ref(arr), 0);
boost::threads th2(addVec, booost::ref(arr), 1);
th1.join();
th2.join();

By now I should have arr[0][0] = 0; and arr[1][0] = 1;
The questions is if this is safe?
Internally the threads should add values to different parts of vector memory, and because it is constructed at the beginning of size 2 only the internal vectors get resized, witch have thread exclusive access.

  • 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-13T21:16:48+00:00Added an answer on June 13, 2026 at 9:16 pm

    Yes, it is safe in your use case, because you don’t modify the shared data. However, I would prefer to pass the reference to the std::vector<short> to each thread to simplify the usage:

    void addVec(std::vector<short> & arr)
    { 
       arr.push_back(some_data);
    }
    
    std::vector<std::vector<short> > arr(2);
    boost::threads th1(addVec, boost::ref(arr[0]));
    boost::threads th2(addVec, booost::ref(arr[1]));
    th1.join();
    th2.join();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array with a structure like this: $arr= Array( array(
Let's say I have this $(document).ready(function() { var array = $.makeArray($('p')); $(array).appendTo(document.body); }); });
Lets say I have several short string: string[] shortStrings = new string[] {xxx,yyy,zzz}; (this
Lets say i have this array of movie directors: var directors = ['Woody Allen',
Okay, this one is pretty complicated... lets say I have an array called $data
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Let's say I have this array, $array = array( 'name' => 'hermet', 'emails' =>
So let's say I have this array: $requiredFruit= @(apple,pear,nectarine,grape) And I'm given a second
Let's say I have an array of objects [{href: 'some_uri', 'class': 'this-css-class'}, {href: 'another_uri',
I'm having a trouble with this little problem. Let's say, I have an array,

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.