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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:14:39+00:00 2026-06-15T10:14:39+00:00

OK so I’ve been told that this code isn’t thread safe. #include <iostream> #include

  • 0

OK so I’ve been told that this code isn’t thread safe.

#include <iostream>
#include <thread>

static int sum[5];

static int get_sum()
{
    int x=0;
    for (int j=0;j<5;++j)
        x += sum[j];
    return x;
}

static void f1(int x){
    sum[x] = 1;
    std::cout << "f" <<x << ": " << sum[x] << " : " << get_sum() << std::endl;
}

int main()  {

    for (int j=0;j<5;++j)
        sum[j] = 0;

    std::thread t0(f1, 0);
    std::thread t1(f1, 1);
    std::thread t2(f1, 2);
    std::thread t3(f1, 3);
    std::thread t4(f1, 4);

    while (get_sum() != 5)  ;

    t0.join();
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    std::cout << "final: " << get_sum() << std::endl;
}

Can someone explain to me why the program might fail to complete? I know the running values of get_sum will be non-deterministic and the output from cout will be randomly interleaved but that’s not relevant to the program completing.

  • 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-15T10:14:40+00:00Added an answer on June 15, 2026 at 10:14 am

    A value may not be accessed in one thread while it might be modified in another. That’s the rules.

    We can speculate about specific problems that might occur on specific platforms. But generally that’s unhelpful because it leads people to think that if they can just fix every possible way they can think of that it might go wrong then their code is okay. But it’s not — things can, and do, go wrong in ways you couldn’t have thought of at the time. Please don’t start thinking this way. The list of failures caused by this type of reasoning is long and doesn’t need to get any longer.

    Here’s one way it can fail: The compiler might inline get_sum and copy all the values into registers before the while loop, causing the loop to repeat forever. This is a legal optimization because the compiler can see that your code could read any of those values at any time during the while loop, thus no other thread is permitted to modify them during that loop.

    Now, if you read this question and the answers, you’ll see that a lot of people couldn’t think of this way the code can go wrong. But that’s okay, they don’t have to. These rules exist because it’s difficult, perhaps even impossible, to think of all the possible ways code might go wrong. So don’t make your code’s correctness rely on you having this super-human ability.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.