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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:34:05+00:00 2026-05-19T01:34:05+00:00

I was randomly testing std::thread in my virtual linux machine (GCC 4.4.5-Debian) with this

  • 0

I was randomly testing std::thread in my virtual linux machine (GCC 4.4.5-Debian) with this test program:

#include <algorithm>
#include <thread>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

static int i=0;
void f( vector<int> &test)
{
    ++i;
    cout << "Push back called" << endl;
    test.push_back(i);
}

int main()
{
    vector<thread> t;
    vector<int> test;
    for( int i=0; i<1000; ++i )
    {
        t.push_back( thread( bind(f, test) ) );
    }
    for( auto it = t.begin(); it != t.end(); ++it )
    {
        (*it).join();
    }
    cout << test.size() << endl;
    for( auto it = test.begin(); it != test.end(); ++it )
    {
        cout << *it << endl;
    }

    return 0;
}

Why does vector test remain empty? Am I doing something stupid with references (probably) or is it something with bind or some threading problem?

Thanks!

UPDATE: with the combined help of Kos and villintehaspan I “fixed” the “problem”:

#include <algorithm>
#include <thread>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

static int i=0;
void f( vector<int> &test)
{
    ++i;
    test.push_back(i);
}

int main()
{
    vector<thread> t;
    vector<int> test;
    for( int i=0; i<1000; ++i )
    {
        t.push_back( thread(f, std::ref(test)) );
    }
    for( auto it = t.begin(); it != t.end(); ++it )
    {
        (*it).join();
    }
    cout << test.size() << endl;
    for( auto it = test.begin(); it != test.end(); ++it )
    {
        cout << *it << endl;
    }

    return 0;
}

Which prints all values in order and seems to work OK. Now only one question remains: is this just lucky (aka undefined behavior (TM) ) or is the static variable causing a silent mutex-like step in the code?

PS: I understand the “killing multithreadedness” problem here, and that’s not my point. I’m just trying to test the robustness of the basic std::thread functionality…

  • 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-19T01:34:06+00:00Added an answer on May 19, 2026 at 1:34 am

    Looks to me like a threading problem.

    While I’m not 100% sure, it should be noted that all 1000 threads:

    • do ++i on the same int value (it’s not an atomic operation- you may encounter problems here, you can use __sync_fetch_and_add(&i,1) instead (note that it’s a gcc extension not standard C++);

    • do push_back simultaneously on a std::vector, which is not a thread-safe container AFAIK… Same for cout I think. I believe you’d need to use a locking mechanism around that (std::mutex perhaps? I’ve only used pthreads so far but I believe it’s what you need).

    Note that this kind of kills any benefit of using threads here, but that’s a consequence of the fact that you shouldn’t use multiple threads at once on a non-thread-safe object.


    —-EDIT—-

    I had a google on this threading API (not present on my tdm gcc 4.5 on Windows, unfortunately).
    Aparrently instead of:

    thread( bind(f, test) )
    

    you can just say

    thread( f, test )
    

    and pass an arbitrary number of arguments in this way.

    Source: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=422

    This should also solve your problem with making a copy of the vector which I haven’t noticed before
    (+1 for @villintehaspam here).


    Actually, one more thing is needed to make sure the copy isn’t created here:

    thread( f, std::ref(test) )
    

    will make sure that the vector isn’t copied.

    Wow, I got confused too. 🙂

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

Sidebar

Related Questions

I am coding up a program for automated testing which randomly clicks an open
I'm using Watir to do some automated testing for a website. This particular test
I wrote a test program for testing Cassandra, and I had problems reading data.
This recent question about sorting randomly using C# got me thinking about the way
One of my C# Winforms applications randomly displays a pure virtual function call message
I randomly get this error, and I can't figure out a way to fix
Im getting this error message randomly: Index was outside the bounds of the array.
I get this error randomly when using Zend_Search_Lucene. Exception thrown without a stack frame
The famous Fisher-Yates shuffle algorithm can be used to randomly permute an array A
Visual Studio randomly crashes when adding/removing references and projects. Any thoughts why? Will installing

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.