Volatile vs. mutable in c ++

My question is about the difference between volatile and volatile. I noticed that both of these tools mean that this can be changed. What else? Are they the same? What's the difference? Where are they applicable? Why are two ideas suggested? How to use them in different ways?

Thank you very much.

+74
c ++ volatile mutable
Mar 15 '10 at 2:11
source share
6 answers
Field

A mutable can be changed even in an object accessed through a pointer or link to const or in a const object, so the compiler does not know how to store it in R / O memory. A volatile is a location that can be changed by code whose compiler is not known (for example, by some kernel-level driver), so the compiler knows that it does not optimize, for example. register the assignment of this value in accordance with the unacceptable assumption that the value "cannot be changed", because it was the last loaded in this register. The very different information provided to the compiler to stop very different kinds of invalid optimizations.

+94
Mar 15 '10 at 2:15
source share

mutable : A mutable keyword overrides any enclosed const statement. The mutable member of the const object can be changed.

volatile : the volatile keyword is an implementation-dependent modifier that is used when declaring variables, which prevents the compiler from optimizing these variables. Volatile should be used with variables whose value may change in unexpected ways (i.e., through an interrupt), which may conflict with optimizations that the compiler can perform.

Source

+25
Mar 15 '10 at 2:15
source share

They are definitely NOT the same. Mutable interacts with const. If you have a const pointer, you usually cannot modify members. Mutable provides an exception to this rule.

Instability, on the other hand, is completely unrelated to changes made by the program. This means that memory can change for reasons beyond the control of the compiler, so the compiler must read or write the memory address each time and not cache the contents in the register.

+21
Mar 15 '10 at 2:14
source share

A crude but effective way of thinking about the difference:

  • The compiler knows when the mutable object changes.
  • The compiler cannot know when a mutable object changes.
+13
Mar 15 '10 at 2:30
source share

A variable labeled mutable allows mutable to modify it in the declared const method.

The variable marked volatile tells the compiler that it should read / write the variable every time your code also reports this (i.e. it cannot optimize access to the variable).

+7
Mar 15 '10 at 2:15
source share

I would like to add that volatile is also very useful when working with multi-threaded applications, i.e. you have a main thread (where main () lives), and you create a work thread that will continue to rotate, and the variable "app_running" is true. main () determines whether app_running is true or false, so if you do not add a mutable attribute to the app_running declaration, if the compiler optimizes access to app_running in the code launched by the secondary thread, main () can change app_running "to false, but the secondary stream will continue to work because the value is cached. I saw the same behavior using gcc for Linux and VisualC ++. The volatile attribute placed in the app_running declaration solved the problem. Thus, this is a scenario in which no hardware interrupts or kernel are used when changing the values โ€‹โ€‹of such variables.

+4
30 Oct. '13 at 18:04 on
source share



All Articles