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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:16:04+00:00 2026-05-29T05:16:04+00:00

Consider the following: int grab_next_target(int* target) { do { /* Intention: store current value

  • 0

Consider the following:

    int grab_next_target(int* target) { 
            do {    
                    /* Intention: store current value of *target into old, so as  
                       to ensure that old never changes */ 
                    int old = *target; 
                    /* get new value based on old -- note that old is assumed not to change here */ 
                    int new; 
                    if (1 == old) { /* imagine that old is 1 so new is now 20 */ 
                            new = 20; 
                    } else if (2 == old) { 
                            new = 300; 
                    } else if (3 == old) { 
                            new = -20; 
                    } else if (4 == old) { 
                            new = 400; 
                    } 
                    /* but the compiler has optimized 
                       old to just read from *target, so *target could be 
                       changed by another thread to be 4.  The CAS will succeed 
                       and now target will hold the wrong value (it will hold 20, instead of 400)  */ 
            } while (!AO_compare_and_swap(target, old, new)); 
    } 

I need a way to read *target into a local variable and ensure that the local variable does not get optimized away to simply be *target. Is volatile an answer?

  • 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-29T05:16:04+00:00Added an answer on May 29, 2026 at 5:16 am

    Yes, that (and exactly that) is what volatile does.

    int grab_next_target(volatile int *target) {
        ...
        int old = *target; // Guaranteed to access "target" exactly once
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: int&& x = 42; static_assert(std::is_same<decltype( x ), int&&>::value, &&); static_assert(std::is_same<decltype((x)),
Consider the following: int ival = 1.01; int &rval = 1.01; // error: non-const
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
Consider the following statements: int *pFarr, *pVarr; int farr[3] = {11,22,33}; int varr[3] =
Consider the following code: int main() { int i; volatile int* p = &i;
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following: typedef struct { int a; int b; int c; int d;
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following example program: next :: Int -> Int next i | 0

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.