When to use – and not use – the volatile keyword
The C language is like a prize-winning race horse. It’s temperamental, stubborn, and unpredictable. And it seems sometimes to have a mind of its own, going its own way based on its interpretation of what the jockey has instructed rather than what the jockey intended. But like the race horse, when in the hands of an experienced and capable master, the C language is capable of doing remarkable things.
But just as the jockey must be careful in how he communicates with his steed – a pressing of the knees, a soft word, or light touch of a spur – so also the programmer has be careful in the commands and features of the C language and when and how to use them.
According to Colin Walls, in his recent column for Embedded.com, (included in this week’s Tech Focus newsletter, one of the more problematic features of the C language the use of C's volatile keyword. While critical to many embedded software applications, it is generally not well understood. In addition, a number of compilers do not implement the keyword's functionality correctly, which compounds the problem.
In his column, Colin does an excellent job of guiding the developer through the maze of choices he or she faces when the situation requires its use.. To provide some additional insight, the newsletter also includes a number of articles and columns on this important topic, of which my Editor’s Top Picks are:
Introduction to the volatile keyword, by Nigel Jones, who also writes a blog at “Stack Overflow.” This early article for Embedded.com is still relevant and a good complement to Colin’s. I go back and read it regularly because it provides much needed information that is still not covered adequately in most books of C programming, (and from the number of them in my office library book case, I must have read all of them!!!.).
Combining C's volatile and const keywords,” by Michael Barr, former Embedded.com editor-in-chief. In it he addresses such continuing questions as: Does it ever make sense to declare a variable in C or C++ as both volatile (in other words, "ever-changing") and const ("read-only")? If so, why? And how should you combine volatile and const properly? (In addition to continuing to write regularly for Embedded.com, he also maintains his “Barr Code” blog at Embedded Gurus.com.)
To give you some additional context as to when and how to use this and other useful features of C and when not to use them, several other Embedded.com design articles and blogs may be useful:
But no matter how prepared you think you are, be sure to pay attention to Colin’s warning in his blog: “The keyword ‘volatile’ solves a problem experienced by many embedded software developers, but it is necessary to methodically identify which variables need to be declared volatile,” he writes, advising caution its use: ”The chosen compiler’s implementation of the keyword needs to be carefully verified. In a multi-threading context, the use of volatile needs particular care, as it may not provide the expected protection.”
One of the challenges – and satisfactions – of this job is the chance it gives me to learn new things that may challenge my preconceptions and/or force me to go back over what I may already think I know and come away with more in depth understanding. Among other things, after reading over this material and preparing this blog, I have come to the conclusion that maybe Michael Barr’s tongue-in-cheek assertion in a past blog is right: “Real men programming in C.”
Embedded.com Site Editor Bernard Cole is also editor of the twice-a-week Embedded.com newsletters as well as a partner in the TechRite Associates editorial services consultancy. He welcomes your feedback. Send an email to firstname.lastname@example.org, or call 928-525-9087.