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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:46:09+00:00 2026-06-12T22:46:09+00:00

In C++11 standard the machine model changed from a single thread machine to a

  • 0

In C++11 standard the machine model changed from a single thread machine to a multi threaded machine.

Does this mean that the typical static int x; void func() { x = 0; while (x == 0) {} } example of optimized out read will no longer happen in C++11?

EDIT: for those who don’t know this example (I’m seriously astonished), please read this: https://en.wikipedia.org/wiki/Volatile_variable

EDIT2:
OK, I was really expecting that everyone who knew what volatile is has seen this example.

If you use the code in the example the variable read in the cycle will be optimized out, making the cycle endless.

The solution of course is to use volatile which will force the compiler to read the variable on each access.

My question is if this is a deprecated problem in C++11, since the machine model is multi-threaded, therefore the compiler should consider concurrent access to variable to be present in the system.

  • 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-12T22:46:10+00:00Added an answer on June 12, 2026 at 10:46 pm

    Whether it is optimized out depends entirely on compilers and what they choose to optimize away. The C++98/03 memory model does not recognize the possibility that x could change between the setting of it and the retrieval of the value.

    The C++11 memory model does recognize that x could be changed. However, it doesn’t care. Non-atomic access to variables (ie: not using std::atomics or proper mutexes) yields undefined behavior. So it’s perfectly fine for a C++11 compiler to assume that x never changes between the write and reads, since undefined behavior can mean, “the function never sees x change ever.”

    Now, let’s look at what C++11 says about volatile int x;. If you put that in there, and you have some other thread mess with x, you still have undefined behavior. Volatile does not affect threading behavior. C++11’s memory model does not define reads or writes from/to x to be atomic, nor does it require the memory barriers needed for non-atomic reads/writes to be properly ordered. volatile has nothing to do with it one way or the other.

    Oh, your code might work. But C++11 doesn’t guarantee it.

    What volatile tells the compiler is that it can’t optimize memory reads from that variable. However, CPU cores have different caches, and most memory writes do not immediately go out to main memory. They get stored in that core’s local cache, and may be written… eventually.

    CPUs have ways to force cache lines out into memory and to synchronize memory access among different cores. These memory barriers allow two threads to communicate effectively. Merely reading from memory in one core that was written in another core isn’t enough; the core that wrote the memory needs to issue a barrier, and the core that’s reading it needs to have had that barrier complete before reading it to actually get the data.

    volatile guarantees none of this. Volatile works with “hardware, mapped memory and stuff” because the hardware that writes that memory makes sure that the cache issue is taken care of. If CPU cores issued a memory barrier after every write, you can basically kiss any hope of performance goodbye. So C++11 has specific language saying when constructs are required to issue a barrier.

    volatile is about memory access (when to read); threading is about memory integrity (what is actually stored there).

    The C++11 memory model is specific about what operations will cause writes in one thread to become visible in another. It’s about memory integrity, which is not something volatile handles. And memory integrity generally requires both threads to do something.

    For example, if thread A locks a mutex, does a write, and then unlocks it, the C++11 memory model only requires that write to become visible to thread B if thread B later locks it. Until it actually acquires that particular lock, it’s undefined what value is there. This stuff is laid out in great detail in section 1.10 of the standard.

    Let’s look at the code you cite, with respect to the standard. Section 1.10, p8 speaks of the ability of certain library calls to cause a thread to “synchronize with” another thread. Most of the other paragraphs explain how synchronization (and other things) build an order of operations between threads. Of course, your code doesn’t invoke any of this. There is no synchronization point, no dependency ordering, nothing.

    Without such protection, without some form of synchronization or ordering, 1.10 p21 comes in:

    The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. Any such data race results in undefined behavior.

    Your program contains two conflicting actions (reading from x and writing to x). Neither is atomic, and neither is ordered by synchronization to happen before the other.

    Thus, you have achieved undefined behavior.

    So the only case where you get guaranteed multithreaded behavior by the C++11 memory model is if you use a proper mutex or std::atomic<int> x with the proper atomic load/store calls.

    Oh, and you don’t need to make x volatile too. Anytime you call a (non-inline) function, that function or something it calls could modify a global variable. So it cannot optimize away the read of x in the while loop. And every C++11 mechanism to synchronize requires calling a function. That just so happens to invoke a memory barrier.

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

Sidebar

Related Questions

Does ntdll.dll come standard with windows XP and Windows Vista? I know that I
I've got a standard LAMP setup on my machine. This morning, it started executing
Say I know the IP address of a machine/device which is using a non-standard
Standard Java does not offer an object implementing the Queue interface on top of
standard power operation ( ** ) in Python does not work for negative power!
The standard specifies (23.4.4.2:5, etc.) that constructing all four ordered associative containers ( map
In standard SQL, is there a way to say: select mubmle as x from
The standard explicitly states that main has two valid (i.e., guaranteed to work) signatures;
I've created a Windows Service that accepts commands from remote machines via WCF. One
I'm attempting to make a PDF from a HTML document, and this document has

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.