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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:54:55+00:00 2026-05-31T05:54:55+00:00

I would like two threads work like this: First thread will append values to

  • 0

I would like two threads work like this:

  1. First thread will append values to vector
  2. Second thread will have read-only access to elements by index

I can make mutex and make deep copy before second thread start reading…. But this way is really slow… How is it possible to make this without mutex? Here: STL vector and thread-safety
I read that it is possible to use std::deque, but it fails like std::vector …

Where I can find append-only container, which don’t realloc data?


I have solved my problem by creating own container GrowVector with operations: adding elements to back, getting size, accessing element by index. It works for 2Billion elements for default, but it can be changed by constructor parameter.

#include <vector>

template<typename T>
class GrowVector
{
    std::vector<std::vector<T> > m_data;
    size_t m_size;

public:
    GrowVector(int chunks = 32768)
        : m_data()
        , m_size(0)
    {
        m_data.reserve(chunks);
        m_data.push_back(std::vector<T>());
        m_data.back().reserve(1 << 16);
    }

    void add(const T & value)
    {
        if (m_data.back().size() == m_data.back().capacity())
        {
            m_data.push_back(std::vector<T>());
            m_data.back().reserve(1 << 16);
        }

        m_data.back().push_back(value);
        m_size++;
    }

    size_t size() const
    {
        return m_size;
    }

    T & operator [] (int i)
    {
        return m_data[i >> 16][i & 0xffff]; 
    }

    const T & operator [] (int i) const
    {
        return m_data[i >> 16][i & 0xffff];    
    }
};

Is my solution safe?

  • 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-05-31T05:54:56+00:00Added an answer on May 31, 2026 at 5:54 am

    Your solution is not thread-safe without a locking mechanism.

    You can use tbb::concurrent_vector or Concurrency::concurrent_vector for multiple insertion and access simultaneously. No extra locking required. It is unsafe to erase elements from those vectors, but you are ok with it I guess.

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

Sidebar

Related Questions

I would like to have an iterator that can be read by multiple threads
I have several A4 PDF documents which I would like (two into one) glue
I would like to compare two dates in javascript. I have been doing some
I would like a batch file to launch two separate programs then have the
I wish to have two threads. The first thread1 occasionally calls the following pseudo
I would like to add rows to DataGridView from two seperate threads. I tried
I have a XAML form that I would like two independent features threaded out
You have two threads, a and b. Thread a is in a forever loop,
I would like my two applications to be able to send strings to each
I would like to provide two different versions of my iPhone app on the

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.