Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usefulness of volatile in concurrent programming since C++11

I've read this and this answer. I've also searched the book C++ Concurrency in Action and found no discussion about volatile nor any example using it. Looks like it's not designed for concurrency at all. So for concurrent programming, is it sufficient to just use atomic, mutex, etc., and forget about volatile? Any cases where volatile may be needed for concurrency issues?

like image 644
Lingxi Avatar asked Feb 18 '18 19:02

Lingxi


1 Answers

No, in C++ the volatile keyword tells the compiler that must not optimize the variable in any way shape or form. This can be useful when dealing with memory that can be changed from outside of your own code e.g a hardware register on a custom board.
For more in depth guide about volatile you should read Volatile vs. volatile By Herb Sutter

like image 99
blazgrom Avatar answered Sep 19 '22 00:09

blazgrom