Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are const volatile objects necessary?

When are const volatile objects necessary in C++?

Note: I do understand the need for pointers to const volatile memory locations, but those don't require the objects themselves to be const or volatile.
I'm asking about objects that are themselves of some const volatile type, for example:

const volatile T obj;

In which situations are these necessary or useful?

like image 676
user541686 Avatar asked Nov 02 '22 16:11

user541686


1 Answers

The situations are rare where when you actually need volatile in c++. volatile is not useful for multithreaded any more. From this website there are only three portable uses of volatile.

Hans Boehm points out that there are only three portable uses for volatile. I'll summarize them here:

  1. marking a local variable in the scope of a setjmp so that the variable does not rollback after a longjmp.
  2. memory that is modified by an external agent or appears to be because of a screwy memory mapping
  3. signal handler mischief

So basically you want to really only use other features for concurrent programming and save volatile for those rare situations

like image 188
aaronman Avatar answered Nov 26 '22 19:11

aaronman