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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:36:36+00:00 2026-06-06T23:36:36+00:00

I’ve got this little problem on thread. int x = 0; add() { x=x+1;

  • 0

I’ve got this little problem on thread.

    int x = 0;
    add() {

      x=x+1;
    }

If we run this in multiple threads, say 4 threads, is the final value of x=4 at every time or it could be 1,2,3 or 4.

Thanks

PS
lets say the atomic operations for the adding is like this,

LOAD A x
ADD A 1 
LOAD x A

Then the final result will be 4. Am I right or what have I get wrong ?

  • 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-06T23:36:37+00:00Added an answer on June 6, 2026 at 11:36 pm

    This is a classical example of data race.

    Now, let’s take a closer look at what add() does:

    add()
    {
       x = x + 1;
    }
    

    This translates to:

    1. Give me the most recent value of X and store it in my private workspace
    2. Add 1 to that value that is stored in my private workspace
    3. Copy what I have in my workspace to the memory that I copied from (that is globally accessible).

    Now, before we go further in explaining this, you have something called context switch, which is the process by which your operating system divides your processor’s time among different threads and processes. This process usually gives your threads a finite amount of processor time (on windows it is about 40 milliseconds) and then interrupts that work, copies everything the processor have in its registers (and by thus preserve it’s state) and switch to the next task. This is called Round-robin task scheduling.

    You have no control over when you’re processing is going to be interrupted and transferred to another thread.

    Now imagine you have two threads doing the same:

    1. Give me the most recent value of X and store it in my private workspace
    2. Add 1 to that value that is stored in my private workspace
    3. Copy what I have in my workspace to the memory that I copied from (that is globally accessible).
    

    and X is equal to 1 before any of them runs.

    The first thread might execute the first instruction and store in it’s private workspace the value of X that was most recent at the time it was working on – 1. Then a context-switch occurs and the operating system interrupts your threads and gives control to the next task in queue, that happens to be the second thread. The second thread also reads the value of X which is equal to 1.

    Thread number two manages to run to completion – it adds 1 to the value it “downloaded” and “uploads” the calculated value.

    The operating system forces a context switch again.

    Now the first thread continues execution at the point where it was interrupted. It will still think that the most recent value is 1, it will increment that value by one and save the result of it’s computation to that memory area. And this is how data races occur. You expect the final result to be 3 but it is 2.

    There are many ways to avoid this problem such as locks/mutexes, compare and swap or atomic operations.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string

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.