Talk:Volatile (computer programming)
This article needs to be completely rewritten for the following reasons:
1) It confuses the problem of reading stale data with the problem of concurrent access. For example, it suggests that mutual exclusion is an alternative to using 'volatile'.
2) It fails to mention the two things that 'volatile' is actually both necessary and sufficient for, code that might be interrupted by a signal and code that uses 'longjmp'.
3) It talks about behavior that might happen to be true on some particular implementation as if it was guaranteed behavior.
4) It fails to mention that 'volatile' contains no atomicity guarantees whatsoever (except for reads of sig_atomic_t variables on single-threaded programs). So even if it guarantees that a change is noticed, it does not guarantee that the right value is read. (Rather than some bits of the old value and some of the new.) Foolishly, the example does not use 'sig_atomic_t'!
No solution short of rewriting will address these problems, IMO.
questionable link to busy waiting for example
The article links to busy waiting for an example of volatile in use: "For an example of the use of volatile in context, see Busy waiting." However, this is immediately followed by these statements:
1) Note that using volatile as a threading primitive is not guaranteed to work in C, C++, or Java versions 1 to 1.4. There are many systems where it will not.
2) In C and C++, volatile was not intended, and it is not implemented on all systems, to be a correct or useful synchronization primitive.
The example currently up on the busy waiting page DOES appear to be using the volatile variable as a threading primitive for synchronization; either the link to the busy waiting example should be removed, or an explanation should be given as to why it is okay for volatile variables to be used for synchronization in the example code. (From my rudimentary understanding, I don't think that the example code is, in fact, safe.) 134.173.66.78 (talk) 03:12, 30 January 2009 (UTC)
volatile pointer
Does C support volatile pointers? I does not mean pointers to volatile data. Same as const pointers (and pointer to constants). --89.49.193.114 20:35, 11 February 2007 (UTC)
Another example
Without use of the keyword "volatile" the following program - if optimized - will likely just print "value=0" because the code incrementing "value" is just optimized away. This is a somewhat interesting case because it does not involve an external modifier, special memory and no (obvious use of) setjmp.
#include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> static volatile sig_atomic_t value; static void sighandler(int sig) { (void) sig; printf("value=%u\n", (unsigned) value); exit(EXIT_SUCCESS); } static void increment(void) { for (;;) value++; } int main(void) { signal(SIGALRM, sighandler); alarm(1); increment(); return 0; }
--82.141.49.88 02:38, 11 March 2007 (UTC)
Not to mention that in the example "foo" can not be changed by a different thread since it is static.
Programming language
It would make sense to mention which programming languages support this feature. The article talks about it as if it were something universal. --CyHawk (talk) 19:45, 22 October 2008 (UTC)
C# also has volatile, which the article fails to mention. 82.79.96.156 (talk) 08:31, 17 July 2009 (UTC)