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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:37:03+00:00 2026-05-26T10:37:03+00:00

On the following code: std::atomic<int> myint; //Shared variable //(…) if( –myint == 0) {

  • 0

On the following code:

std::atomic<int> myint; //Shared variable
//(...)
if( --myint == 0) {
    //Code block B
}

Is it possible that more than one thread access the block I named “Code Block B”?

Please consider that overflow will not happen, that the ‘if’ is being executed concurrently by more than one thread, that the only modification to myint in the whole program is the –myint inside the if and that myint is initialized with a positive value.

  • 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-26T10:37:04+00:00Added an answer on May 26, 2026 at 10:37 am

    C++0x paper N2427 (atomics) states roughly the following. I’ve changed the wording slightly so its easier to read for the specific decrement situation, the parts I changed are in bold:

    Effects:
    Atomically replace the value in object with result of the decrement applied to the value in object and the given operand. Memory is affected as per order. These operations are read-modify-write operations in the sense of the “synchronizes with” definition in [the new section added by N2334 or successor], and hence both such an operation and the evaluation that produced the input value synchronize with any evaluation that reads the updated value.

    Returns:
    Atomically, the value of object immediately before the decrement.

    The atomic operation guarantees that the decrement operator will return the value that the variable held immediately before the operation, this is atomic so there can be no intermediate value from updates by another thread.

    This means the following are the only possible executions of this code with 2 threads:

    (Initial Value: 1)
    Thread 1: Decrement 
    Thread 1: Compare, value is 0, enter region of interest
    Thread 2: Decrement
    Thread 2: Compare, value is -1, don't enter region
    

    or

    (Initial Value: 1)
    Thread 1: Decrement 
    Thread 2: Decrement
    Thread 1: Compare, value is 0, enter region of interest
    Thread 2: Compare, value is -1, don't enter region
    

    Case 1 is the uninteresting expected case.

    Case 2 interleaves the decrement operations and executes the comparison operations later. Because the decrement-and-fetch operation is atomic, it is impossible for thread 1 to receive a value other than 0 for comparison. It cannot receive a -1 because the operation was atomic… the read takes place at the time of the decrement and not at the time of the comparison. More threads will not change this behavior.

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

Sidebar

Related Questions

The following code works fine : std::map<int, int>& m = std::map<int, int>(); int i
The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
Consider the following code: typedef std::vector<int> cont_t; // Any container with RA-iterators typedef cont_t::const_iterator
In the book 'C++ In A Nutshell', there is the following example code std::vector<int>
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
In the following code: std::vector<int> var; for (int i = 0; i < var.size();
The following code leads to a memory leakage: std::list<float*> vertices; float* v; for (int
I am using the following code: #include <iostream> using namespace std; int main(int argc,

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.