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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:57:36+00:00 2026-06-10T20:57:36+00:00

In my program I’ve some threads running. Each thread gets a pointer to some

  • 0

In my program I’ve some threads running. Each thread gets a pointer to some object (in my program – vector). And each thread modifies the vector.

And sometimes my program fails with a segm-fault. I thought it occurred because thread A begins doing something with the vector while thread B hasn’t finished operating with it? Can it be true?

How am I supposed to fix it? Thread synchronization? Or maybe make a flag VectorIsInUse and set this flag to true while operating with it?

  • 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-10T20:57:37+00:00Added an answer on June 10, 2026 at 8:57 pm

    vector, like all STL containers, is not thread-safe. You have to explicitly manage the synchronization yourself. A std::mutex or boost::mutex could be use to synchronize access to the vector.

    Do not use a flag as this is not thread-safe:

    • Thread A checks value of isInUse flag and it is false
    • Thread A is suspended
    • Thread B checks value of isInUse flag and it is false
    • Thread B sets isInUse to true
    • Thread B is suspended
    • Thread A is resumed
    • Thread A still thinks isInUse is false and sets it true
    • Thread A and Thread B now both have access to the vector

    Note that each thread will have to lock the vector for the entire time it needs to use it. This includes modifying the vector and using the vector‘s iterators as iterators can become invalidated if the element they refer to is erase() or the vector undergoes an internal reallocation. For example do not:

    mtx.lock();
    std::vector<std::string>::iterator i = the_vector.begin();
    mtx.unlock();
    
    // 'i' can become invalid if the `vector` is modified.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Program runs once and it both throws data to the pipe and gets it
This program runs just fine, but for some reason when I ask for an
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread -177935456 (LWP 5483)] 0xf79ff2ca in
My program is not doing a great job. In a loop, data from each
My program terminates for some reason. Its a rock-paper-scisors simulation. The termination takes place
My program sets its display based on if the program is running for the
My program processes PDF files and reads some streams out of them. There are
Most program languages have some kind of exception handling; some languages have return codes,
A program receives a list of Messages (base type). Each message in the list
class Program { static object test = new object(); static void Main(string[] args) {

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.