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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:13:21+00:00 2026-06-04T13:13:21+00:00

I am iterating over an STL vector and reading values from it. There is

  • 0

I am iterating over an STL vector and reading values from it. There is another thread which can make changes to this vector. Now, if the other thread inserts or removes and element from the vector, it invalidates the iterator. There is no use of locks involved. Does my choice of accessing the container through indexes(Approach 1) in place of iterators(Approach 2) make it thread safe? What about performance?

struct A{int i; int j;};

Approach 1:

   size_t s = v.size();//v contains pointers to objects of type A
    for(size_t i = 0; i < s; ++i)
    {
         A* ptr = v[i];
         ptr->i++;
    }

Approach 2:

std::vector<A*>::iterator begin =  v.begin();
std::vector<A*>::iterator end =  v.end();
for(std::vector<A*>::iterator it = begin; it != end; ++it)
{
     A* ptr = *it;
     ptr->i++: 
}
  • 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-04T13:13:22+00:00Added an answer on June 4, 2026 at 1:13 pm

    OP “Does my choice of accessing the container through indexes(Approach 1) in place of iterators(Approach 2) make it thread safe?”

    No, neither approach is thread safe once you start writing to your data structure.

    Therefore you will need to serialize access to your data structure.

    To save you a lot of time and frustration there a lot of ready-rolled solutions e.g.

    Intel Threading Building Blocks (TBB) which comes with thread safe containers such as concurrent_vector.

    http://threadingbuildingblocks.org/

    A concurrent_vector is a container with the following features:

    • Random access by index. The index of the first element is zero.
    • Multiple threads can grow the container and append new elements concurrently.
    • Growing the container does not invalidate existing iterators or indices.*

    OP “What about performance?”

    Not knowable. Different performance on different systems with different compilers but not known to be large enough to influence your choices.

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

Sidebar

Related Questions

I'm iterating over a 3 dimensional array (which is an image with 3 values
I am iterating over, and modifying a map (which is created from an existing
Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
I'm iterating over a JRE Collection which enforces the fail-fast iterator concept, and thus
Is there a better shorter way than iterating over the array? int[] arr =
Starting from Visual Studio 2010, iterating over a set seems to return an iterator
I need a function, which is capable of iterating over the collection, calling a
To access the values id1 & id2 im iterating over every value in the
I have a class (foo) that contains a vector. If i try iterating over
I'm iterating over a very large set of strings, which iterates over a smaller

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.