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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:52:14+00:00 2026-05-28T05:52:14+00:00

I searched around a bit, but could not find anything useful. Could someone help

  • 0

I searched around a bit, but could not find anything useful. Could someone help me with this concurrency/synchronization problem?

Given five instances of the program below running asynchronously, with s being a shared data with an initial value of 0 and i a local variable, which values are obtainable by s?

for (i = 0; i < 5; i ++) {
    s = s + 1;
}
  1. 2
  2. 1
  3. 6

I would like to know which values, and why exactly.

  • 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-28T05:52:14+00:00Added an answer on May 28, 2026 at 5:52 am

    The non-answering answer is: Uaaaagh, don’t do such a thing.

    An answer more in the sense of your question is: In principle, any value is possible, because it is totally undefined. You have no strict guarantee that concurrent writes are atomic in any way and don’t result in complete garbage.
    In practice, less-than-machine-word sized writes are atomic everywhere (for what I know, at least), but they do not have a defined order. Also, you usually don’t know in which order threads/processes are scheduled. So you will never see a “random garbage” value, but also you cannot know what it will be. It will be anything 5 or higher (up to 25).

    Since no atomic increment is used, there is a race between reading the value, incrementing it, and writing it back. If the value is being written by another instance before the result is written back, the write (and thus increment) that finished earlier has no effect. If that does not happen, both increments are effective.

    Nevertheless, each instance increments the value at least 5 times, so apart from the theoretical “total garbage” possibility, there is no way a value less than 5 could result in the end. Therefore (1) and (2) are not possible, but (3) is.

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

Sidebar

Related Questions

I searched around a little bit for information on this but didn't find anything
I've searched around for a bit, but haven't been able to find anything useful
I've searched around but I can't find any information about this; maybe I'm not
I have searched around for an answer to this but can't find one. When
I searched around a bit and couldn't find anything clear cut as to whether/how
I have searched around a bit, and have not really found a professional type
I've searched around quite a bit for ways to do this and I finally
I've searched around SO for this and found a few things, but I'm still
I have searched around Google and StackOverflow trying to find a solution to this,
I've searched around a bit, but haven't found a satisfactory answer, so I'd like

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.