Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Volatile modifier (was: gcc compiler bad behaviour)

2012-04-16 by bayramdavies

Group,

While composing my previous post in reply to Bob Paddock, I read the Dr. Dobb's Journal article he referenced only as far as was necessary to determine that it was not about my algorithm (which demonstrates passing data between threads using only volatile control variables and without locks).  I have now read it in its entirety.  Although it is hard going, I think I understand it.

Herb Sutter's criticisms of the code he dissects center on the control variables used to keep track of the position of the data item due for next removal and the data item most recently inserted into the data queue.  He shows that they are of a type that does not guarantee atomic access.  He further states that the compiler is free to re-order object access around these types.  Both are fatal problems.

Interestingly, these are exactly the two problems I explicitly avoid in my algorithm.  If you re-read my description, you will see that I require access to the control variables (read and write counters) to be "inherently atomic", which in effect means that they are each a single machine word, 8 bits in the case of an AVR.  Second, I follow Don Kinzer's advice by declaring them volatile.  The compiler is not allowed to re-order object access on either side of an access to a volatile variable.

This article is, therefore, pretty much a direct counter-example to Bob's assertion that "Volatile is never the solution to a problem with threads".  Half of the problem exposed in the article is precisely that the control variables do not have the quality that the volatile qualifier would give them.

Graham Davies
ECROS Technology
www.ecrostech.com

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.