C volatile variables and Cache Memory

Firmware developer here. This is a standard problem in embedded programming, and one that trips up many (even very experienced) developers. My assumption is that you are attempting to access a hardware register, and that register value can change over time (be it interrupt status, timer, GPIO indications, etc.). The volatile keyword is only part …

Read more

Why is volatile deprecated in C++20?

There’s a good talk by the C++ committee language evolution chair on why. Brief summary, the places that volatile is being removed from didn’t have any well defined meaning in the standard and just caused confusion. Motivating (Ambiguous) Examples Volatile bit Fields should be specified by your hardware manual and/or compiler. Is += a single/atomic …

Read more

What is the purpose of the “volatile” keyword appearing inside an array subscript?

The volatile keyword is used to declare an array type of a function parameter. Here, double x[volatile] is equivalent to double * volatile x. The cppreference says : In a function declaration, the keyword volatile may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies …

Read more

Are volatile variable ‘reads’ as fast as normal reads?

You should really check out this article: http://brooker.co.za/blog/2012/09/10/volatile.html. The blog article argues volatile reads can be a lot slower (also for x86) than non-volatile reads on x86. Test 1 is a parallel read and write to a non-volatile variable. There is no visibility mechanism and the results of the reads are potentially stale. Test 2 …

Read more