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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:15:05+00:00 2026-05-22T14:15:05+00:00

Possible Duplicate: Are incrementers / decrementers (var++, var–) etc thread safe? Can you describe

  • 0

Possible Duplicate:
Are incrementers / decrementers (var++, var–) etc thread safe?

Can you describe for me, at the assembly code level, why incrementing a value from two different threads is not considered safe on a single core machine?

  • 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-22T14:15:05+00:00Added an answer on May 22, 2026 at 2:15 pm

    Consider the instructions that might be generated for a statement like i++. Of course this will depend upon your architecture/instruction set, but it will probably be something along the lines of:

    LOAD    @i, r0    ;load the value of 'i' into a register from memory
    ADD     r0, 1     ;increment the value in the register
    STORE   r0, @i    ;write the updated value back to memory
    

    Now consider how multithreading would be implemented in an operating-system, regardless of how many cores the machine has. At the most basic level, the OS is going to need some facility to interrupt the execution of the current thread, save its state, and perform a context switch to a different thread. The OS has no automatic way of knowing which instructions inside of a user thread should be treated as an atomic operation, and has the ability to initiate a context switch between any two instructions.

    So what happens if the OS performs a context switch from one thread to the other between LOAD and ADD? Let’s say that i started out with a value of 0, so r0 will be set to 0 when the first thread gets swapped out. The OS will save this value as part of that thread’s state. Now the second thread runs, and performs the same LOAD statement. The value in memory is still 0, so r0 gets 0 loaded into it again. The thread increments the value and writes it back to memory, setting the value of i to 1. Now the first thread resumes execution, and the operating-system restores the value of r0 to 0 as part of its context switch. The first thread now performs its increment, setting r0 to 1, and the value of 1 gets stored in i again. Now the value of i is incorrect because two increments have been applied, but the value has only increased by 1.

    So in a nutshell, even though i++ is a single statement in a high-level language, it generates multiple assembly-language instructions, and those instructions will not be treated as atomic by the operating-system/runtime environment unless you add extra synchronization logic around them.

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

Sidebar

Related Questions

Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Realloc is not resizing array of pointers Can anyone tell me where
Possible Duplicate: How to convert text to unicode code point like \u0054\u0068\u0069\u0073 using php?
Possible Duplicate: I have a nested data structure / JSON, how can I access
Possible Duplicate: Incrementing a field in SQL using PHP I have a table (T1)
Possible Duplicate: Java hangman game with gui, problems with incrementing/decrementing numbers So the problem

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.