LWN: Comments on "ACCESS_ONCE()" https://lwn.net/Articles/508991/ This is a special feed containing comments posted to the individual LWN article titled "ACCESS_ONCE()". en-us Thu, 25 Sep 2025 06:42:23 +0000 Thu, 25 Sep 2025 06:42:23 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net ACCESS_ONCE() https://lwn.net/Articles/632969/ https://lwn.net/Articles/632969/ gmaxwell <div class="FormattedComment"> Typeof here isn't important to whats being done. Absent typeof you'd just have ACCESS_INT_ONCE() (or use the volatile cast directly instead of via a macro); the use of typeof here just makes the code more tidy and allows using a macro to express the programmer's intent better to other programmers.<br> </div> Wed, 11 Feb 2015 22:00:41 +0000 ACCESS_ONCE() https://lwn.net/Articles/624639/ https://lwn.net/Articles/624639/ cborni <div class="FormattedComment"> wow, I posted this into the wrong thread. Please disregard this here.<br> </div> Thu, 04 Dec 2014 20:01:54 +0000 ACCESS_ONCE() https://lwn.net/Articles/624535/ https://lwn.net/Articles/624535/ cborni <div class="FormattedComment"> Actually the old ACCESS_ONCE code was supposed to work:<br> <p> While it is not that obvious the C99 standard basically forbids<br> duplicating the memory access in that case. For an argumentation of<br> a similiar case please see:<br> <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=22278#c43">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=22278#c43</a><br> <p> For the implementation-defined cases regarding volatile there are some<br> GCC-specific clarifications which can be found here:<br> <a href="https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html#Volatiles">https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html#Volatiles</a><br> <p> This issue was reported as bug and fixed (as a bug)<br> <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145</a>"<br> <p> SO strictly speaking it was a gcc bug<br> </div> Thu, 04 Dec 2014 15:27:46 +0000 Declaring it volatile https://lwn.net/Articles/583578/ https://lwn.net/Articles/583578/ mathstuf <div class="FormattedComment"> Maybe a better name would be FORCE_ACCESS_HERE().<br> </div> Fri, 31 Jan 2014 04:07:16 +0000 Declaring it volatile https://lwn.net/Articles/583536/ https://lwn.net/Articles/583536/ timur <div class="FormattedComment"> Hmm ... I think I get it. Since lock-&gt;owner is temporarily declared volatile, the compiler forces a load at that point in the code. But 'owner' is not declared volatile, so the compiler doesn't need to reload it, and it has "forgotten" about lock-&gt;owner, so it never tries to reload it, until the loop restarts.<br> <p> Sneaky.<br> </div> Thu, 30 Jan 2014 22:55:19 +0000 Declaring it volatile https://lwn.net/Articles/583531/ https://lwn.net/Articles/583531/ timur <i>The functionality of this macro is actually well described by its name</i> <br><br> I disagree. I was completely confused by the article until I saw the implementation of ACCESS_ONCE, because the behavior being described is the opposite of what I think "access once" means. ACCESS_ONCE makes me believe that that it operates like pr_info_once(), which literally calls pr_info only once, no matter how many times loop repeats. I also don't see how declaring it volatile forces the compiler to reload the variable only at the top of the loop. Since it's declared volatile, the compiler can reload 'owner' any time it wants. Thu, 30 Jan 2014 22:39:31 +0000 ACCESS_ONCE() https://lwn.net/Articles/512404/ https://lwn.net/Articles/512404/ etienne <div class="FormattedComment"> The thing is, can the type returned by typeof() be modified by adding the "volatile" keyword, how much can it be modified (how to remove volatility, possible to change the sign-ness by adding signed/unsigned)...<br> </div> Mon, 20 Aug 2012 10:07:45 +0000 ACCESS_ONCE() https://lwn.net/Articles/512352/ https://lwn.net/Articles/512352/ PaulMcKenney <div class="FormattedComment"> ACCESS_ONCE() is simply the macro called out in the article, which simply uses the volatile keyword in a cast, which is part of standard C.<br> </div> Sun, 19 Aug 2012 19:38:21 +0000 ACCESS_ONCE() https://lwn.net/Articles/512024/ https://lwn.net/Articles/512024/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; the spec says it can safely do so.</font><br> <p> But does the spec say it _has_ to do so? Does it more good or harm?<br> <p> Not everything that is allowed is good. For example, the compiler is (in theory) allowed to do anything it wants when presented with code that raises undefined behavior. Anything. "rm -rf /" for instance would be 100% correct. Are GCC developers planing this "feature" for gcc 4.8? Of course not, that would be stupid when you could be playing nethack instead...<br> </div> Fri, 17 Aug 2012 10:57:50 +0000 ACCESS_ONCE() https://lwn.net/Articles/512021/ https://lwn.net/Articles/512021/ dgm <div class="FormattedComment"> typeof() is clearly something acceptable in gcc's C dialect. Or have they removed their own extension without the word noticing?<br> </div> Fri, 17 Aug 2012 10:37:05 +0000 ACCESS_ONCE() https://lwn.net/Articles/511717/ https://lwn.net/Articles/511717/ quanstro <div class="FormattedComment"> i agree with what you say, but that's not the point i'm trying to make.<br> (and btw, i don't think that ACCESS_ONCE is standard c. nor can the<br> kernel be compiled with an arbitrary compiler; it depends on gcc.)<br> <p> for me, making code safe from all possible according-to-hoyle legal<br> transformations of the code is not really interesting or useful.<br> i'd much rather focus on having a tractable, easy-to-use programming<br> environment.<br> <p> if restricting the compiler from making some theoretically legal<br> code transformations reduces bugs and generally makes life easier,<br> then why not do it?<br> <p> as it is i believe there are some gcc optimizations that can break the<br> kernel.<br> <p> </div> Thu, 16 Aug 2012 14:52:04 +0000 ACCESS_ONCE() https://lwn.net/Articles/511042/ https://lwn.net/Articles/511042/ jwakely <div class="FormattedComment"> <font class="QuotedText">&gt; Now, gcc has for example the -pthreads switch that tells it something about what to expect so that can help.</font><br> <p> Except that for many platforms it's equivalent to '-D_REENTRANT -lpthread' and so only affects the preprocessor and linker, which doesn't tell the compiler anything.<br> </div> Mon, 13 Aug 2012 16:58:06 +0000 ACCESS_ONCE() https://lwn.net/Articles/511031/ https://lwn.net/Articles/511031/ nybble41 <div class="FormattedComment"> Fortunately, in your example avar is always initialized to 10. :)<br> <p> Assuming "loglevel = 4" was replaced with "loglevel == 4", I would expect the compiler to generate a warning in this case, since it can't _prove_ that avar was initialized before the printf() call. I would also hope that the compiler would take advantage of the fact that avar is either 10 or undefined to simply set it to 10 regardless of loglevel, for code size and performance reasons if nothing else.<br> <p> In cases like this, IMHO, it would be better to use a common flag rather than testing loglevel multiple times:<br> <p> void fct (void) {<br> int avar;<br> bool use_avar;<br> use_avar = (loglevel == 4);<br> if (use_avar) avar = 10;<br> increase_loglevel();<br> do_some_unrelated_stuff();<br> if (use_avar) printf ("%d\n", avar);<br> }<br> <p> I'm not sure whether the compiler can follow this any better than before, so there may still be a warning, but at least the coupling is visible to anyone reading the code, and doesn't depend on the implementation of external functions.<br> </div> Mon, 13 Aug 2012 16:50:20 +0000 ACCESS_ONCE() https://lwn.net/Articles/511030/ https://lwn.net/Articles/511030/ daglwn <div class="FormattedComment"> <font class="QuotedText">&gt; Threads are supposed to share all memory, aren't they?</font><br> <p> It depends on the threading model and since the standard doesn't specify one, the compiler cannot know in the general case.<br> <p> Now, gcc has for example the -pthreads switch that tells it something about what to expect so that can help. But the kernel build system almost certainly doesn't use that.<br> <p> <font class="QuotedText">&gt; There are plenty of opportunities for optimization, but I don't think </font><br> <font class="QuotedText">&gt; this is an example of one.</font><br> <p> When one restricts what is know about memory accesses, the compiler kills a *lot* of compiler transformation. Much much more than one would think. Spill code gets a lot more inefficient, for example, which you wouldn't expect to happen since spills are entirely local. But it does because the compiler cannot do certain peeps that can help reduce the amount of spill code.<br> <p> The "big" transformations like loop restructuring, vectorization and the like are almost impossible if the behavior of memory can't be determined. These are no more "unsafe" than any other transformation. The compiler won't do them if it doesn't know it's safe according to the standard.<br> <p> </div> Mon, 13 Aug 2012 16:41:56 +0000 ACCESS_ONCE() https://lwn.net/Articles/511028/ https://lwn.net/Articles/511028/ daglwn <div class="FormattedComment"> Yes, it is. It uses typeof().<br> <p> But ignoring that, nye has the more useful response. :)<br> <p> </div> Mon, 13 Aug 2012 16:35:25 +0000 ACCESS_ONCE() https://lwn.net/Articles/511027/ https://lwn.net/Articles/511027/ daglwn <div class="FormattedComment"> It is not trivial to get an exact and accurate answer in the general case, true, but I was assuming we were talking about the common case of locally-declared variables.<br> <p> Still, even in this case the compiler could warn about it even if it doesn't know for sure. The code certainly looks suspicious and a warning would be appropriate. False positives are just fine if they are limited in number. The compiler can provide directives to suppress them if the programmer knows it's not a problem.<br> <p> Note that gcc does just this. It warns that variables *might* be uninitialized.<br> <p> </div> Mon, 13 Aug 2012 16:34:21 +0000 ACCESS_ONCE() https://lwn.net/Articles/511026/ https://lwn.net/Articles/511026/ daglwn <div class="FormattedComment"> Absolutely. One needs to first understand _why_ the warning is there before fixing it. Warnings don't replace understanding.<br> <p> </div> Mon, 13 Aug 2012 16:31:38 +0000 ACCESS_ONCE() https://lwn.net/Articles/510979/ https://lwn.net/Articles/510979/ nye <div class="FormattedComment"> <font class="QuotedText">&gt;&gt; How can the compiler possibly know what you meant when you wrote something outside the language spec?</font><br> <p> <font class="QuotedText">&gt; The code we are arguing about, the one with ACCESS_ONCE(), is NOT outside the spec in any way, is it?</font><br> <p> No it isn't. The point is that, absent the ACCESS_ONCE() macro, the assumption that the compiler shouldn't pull that access out of the loop is what's outside the language spec, because the spec says it can safely do so.<br> </div> Mon, 13 Aug 2012 12:11:58 +0000 ACCESS_ONCE() https://lwn.net/Articles/510978/ https://lwn.net/Articles/510978/ nye <div class="FormattedComment"> <font class="QuotedText">&gt;in the example given in the op, there was no reason for the read to be in the</font><br> <font class="QuotedText">&gt; loop, except if it might change. the compiler made the assumption that the</font><br> <font class="QuotedText">&gt; coder was wrong. that might not be a useful assumption.</font><br> <p> No, the coder *was* wrong, and the assumption is *always correct in standard C*. That's the point. The programmer might have assumed semantics which are not C, but the compiler merely assumed that the programmer was writing in C, not writing in some unspecified language that looks a lot *like* C and exists only in the programmer's head.<br> <p> It is axiomatic that a valid optimisation (ie. one which precisely follows C semantics, and any which don't are buggy and tend to be quickly fixed) cannot break correct valid C; if code breaks then it is because the programmer has made incorrect assumptions about the exact meaning *in C* of what they're writing.<br> <p> If your variable might change between accesses, then you need to tell the compiler that, because it is not the case in the standard C model, which is why there's a keyword existing specifically for that purpose.<br> </div> Mon, 13 Aug 2012 12:05:11 +0000 ACCESS_ONCE() https://lwn.net/Articles/510969/ https://lwn.net/Articles/510969/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; And any variable might be shared.</font><br> <p> Threads are supposed to share all memory, aren't they?<br> <p> <font class="QuotedText">&gt;You've just killed all compiler optimization.</font><br> <p> Only the unsafe ones. There are plenty of opportunities for optimization, but I don't think this is an example of one.<br> </div> Mon, 13 Aug 2012 10:07:51 +0000 ACCESS_ONCE() https://lwn.net/Articles/510967/ https://lwn.net/Articles/510967/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; How can the compiler possibly know what you meant when you wrote something outside the language spec?</font><br> <p> The code we are arguing about, the one with ACCESS_ONCE(), is NOT outside the spec in any way, is it? <br> </div> Mon, 13 Aug 2012 09:56:56 +0000 ACCESS_ONCE() https://lwn.net/Articles/510966/ https://lwn.net/Articles/510966/ etienne <div class="FormattedComment"> It is not trivial, or even possible, to detect if a variable is only initialised and used when another variable is set to a special value.<br> Something like (very simplified):<br> extern unsigned loglevel;<br> <p> void fct (void) {<br> int avar;<br> if (loglevel = 4) avar = 10;<br> increase_loglevel();<br> do_some_unrelated_stuff();<br> if (loglevel == 5) printf ("%d\n", avar);<br> }<br> </div> Mon, 13 Aug 2012 09:38:32 +0000 ACCESS_ONCE() https://lwn.net/Articles/510958/ https://lwn.net/Articles/510958/ jezuch <div class="FormattedComment"> <font class="QuotedText">&gt; C compilers detect it all the time. It is a trivial analysis. Users tend to ignore the warnings, however.</font><br> <p> It is. And they do. And every time I see someone fixing a "use of undefined variable" warning by initializing it to zero at declaration point, I cringe. I've seen it in stable updates to the kernel a lot...<br> </div> Mon, 13 Aug 2012 08:02:29 +0000 ACCESS_ONCE() https://lwn.net/Articles/510931/ https://lwn.net/Articles/510931/ Wol <div class="FormattedComment"> Which is why, when I gave a bunch of novice programmers instruction about how to maintain code, I said "the standard is (a) always turn warnings to max and (b) always fix or otherwise understand *every* warning".<br> <p> We had a bunch of warnings we couldn't get rid of, hence it didn't say "fix all warnings", but "explain it away" is just as effective, if less satisfying.<br> <p> Cheers,<br> Wol<br> </div> Sun, 12 Aug 2012 18:52:14 +0000 ACCESS_ONCE() https://lwn.net/Articles/510807/ https://lwn.net/Articles/510807/ quanstro <div class="FormattedComment"> in the example given in the op, there was no reason for the read to be in the<br> loop, except if it might change. the compiler made the assumption that the<br> coder was wrong. that might not be a useful assumption.<br> <p> as i see it, the trade-off here is non-standard constructions, and the<br> principle of least surprise for performance.<br> <p> i'm not convinced of the claim that this is always a win. <br> <p> the compiler i use on a daily basis does not do strength reduction or optimize<br> away indirection. it assumes you know what you're doing. i don't notice that<br> it is slower. i also don't have to worry that the compiler will break my<br> drivers by "optimizing" them.<br> <p> (never mind that with modern intel cpus, strength reduction can be a loss due<br> to microop caching.)<br> <p> i think this is a good trade off for my case because it avoids obtuse, and<br> non-portable constructions that can be hard to remember to apply. that is,<br> for most code, developer time is more expensive than run time.<br> <p> just my two cents, and i realize that there are cases in the linux kernel<br> where complex macros need this sort of optimization. but perhaps that's<br> complexity begetting itself.<br> <p> </div> Sun, 12 Aug 2012 16:16:03 +0000 ACCESS_ONCE() https://lwn.net/Articles/510670/ https://lwn.net/Articles/510670/ nix <blockquote> personally, i find this sort of code reorg by compilers to be problematic as it generally makes code quite hard to reason about. </blockquote> I'm curious. Why don't you say the same about register allocation? Combined with stack spilling, that can often have the same effect as code motion. How do you plan to get rid of that? <p> (I've seen this from various safety-critical embedded people too: they want the compiler to 'not optimize'. I've tried pointing out that this is a meaningless request, that translation necessarily implies many of the same transformations that optimization does, but they never seem to get it. What they generally *mean* is that they want the smallest possible number of transformations -- or perhaps that they want the transformations to be guaranteed bug-free.) Fri, 10 Aug 2012 17:33:36 +0000 ACCESS_ONCE() https://lwn.net/Articles/510602/ https://lwn.net/Articles/510602/ daglwn <div class="FormattedComment"> <font class="QuotedText">&gt; the claim above is that the choice is to</font><br> <font class="QuotedText">&gt; (a) follow the standard, or</font><br> <font class="QuotedText">&gt; (b) do something arbitrary.</font><br> <p> This is a false choice. (a) and (b) are the same thing in the presence of undefined/unspecified/implementation-defined behavior. And it's not arbitrary. It's the decision of the compiler engineers.<br> <p> <font class="QuotedText">&gt; personally, i find this sort of code reorg by compilers</font><br> <font class="QuotedText">&gt; to be problematic as it generally makes code quite hard</font><br> <font class="QuotedText">&gt; to reason about. experienced developers would lift the</font><br> <font class="QuotedText">&gt; assignment out of the loop if it mattered, and it were</font><br> <font class="QuotedText">&gt; legal.</font><br> <p> I hear this a lot. Then people get surprised when I show them what the compiler did to their code. Believe me, there is no reason anyone should waste time hand-optimizing code without proof of need. Either they're going to miss a lot of opportunity or they are going to screw things up and make the compiler's job harder.<br> <p> If a developer were to hand-optimize code to achieve the same performance result, the source code would be unmaintainable.<br> <p> We want optimizing compilers. I can't believe anyone would suggest otherwise.<br> <p> </div> Fri, 10 Aug 2012 15:40:19 +0000 ACCESS_ONCE() https://lwn.net/Articles/510601/ https://lwn.net/Articles/510601/ daglwn <div class="FormattedComment"> C compilers detect it all the time. It is a trivial analysis. Users tend to ignore the warnings, however.<br> <p> </div> Fri, 10 Aug 2012 15:35:09 +0000 ACCESS_ONCE() https://lwn.net/Articles/510583/ https://lwn.net/Articles/510583/ quanstro <div class="FormattedComment"> the claim above is that the choice is to<br> (a) follow the standard, or<br> (b) do something arbitrary.<br> <p> that is not the choice in this case. the choice is to<br> either<br> (a) rearrange the code in optimization, or<br> (b) leave it as the developer wrote it.<br> <p> personally, i find this sort of code reorg by compilers<br> to be problematic as it generally makes code quite hard<br> to reason about. experienced developers would lift the<br> assignment out of the loop if it mattered, and it were<br> legal.<br> <p> </div> Fri, 10 Aug 2012 14:58:25 +0000 ACCESS_ONCE() https://lwn.net/Articles/510542/ https://lwn.net/Articles/510542/ jezuch <div class="FormattedComment"> <font class="QuotedText">&gt; Take a very simple but common issue: undefined data.</font><br> <p> Undefined data, hah. In Java it is a compilation error to use a variable that is not provably assigned in all possible code paths between declaration and use. I like this feature *a lot* and I'm always surprised that C compilers have such a great difficulty with detecting it. (The JVM verifier does this analysis as well during class loading, so it has to be *fast* as well as correct.)<br> </div> Fri, 10 Aug 2012 07:40:10 +0000 ACCESS_ONCE() https://lwn.net/Articles/510338/ https://lwn.net/Articles/510338/ mmorrow <blockquote> Take a very simple but common issue: undefined data. There are countless compiler analyses and transformations that mode code around, reallocate stack space, etc. that cause the undefined variable to have different (garbage) values. What might happen to work one day most assuredly will not when compiled with a compiler 2-3 versions newer. </blockquote> A good example of this can be seen in GCC's constant propagation implem (tree-ssa-ccp.c): <blockquote> <pre> /* ... - If an argument has an UNDEFINED value, then it does not affect the outcome of the meet operation. If a variable V_i has an UNDEFINED value, it means that either its defining statement hasn't been visited yet or V_i has no defining statement, in which case the original symbol 'V' is being used uninitialized. Since 'V' is a local variable, the compiler may assume any initial value for it. ... */ </pre> </blockquote> And we can see this in action with e.g. <blockquote> <pre> size_t f(int x){size_t a; if(x) a = 42; return a;} </pre> </blockquote> which gives <blockquote> <pre> f: movl $42, %eax ret .ident "GCC: (GNU) 4.8.0 20120408 (experimental)" </pre> </blockquote> Thu, 09 Aug 2012 03:47:44 +0000 ACCESS_ONCE() https://lwn.net/Articles/510245/ https://lwn.net/Articles/510245/ daglwn <div class="FormattedComment"> <font class="QuotedText">&gt; It's not the code that it's harmful, but the transformation the compiler </font><br> <font class="QuotedText">&gt; does in the optimization.</font><br> <p> No. The code is the specification of what the programmer wants to happen. If the programmer doesn't mark something "volatile" when it should be, there's no way the compiler can automatically infer that information.<br> <p> <font class="QuotedText">&gt; Because there's no way it can be sure if the transformation is safe (or </font><br> <font class="QuotedText">&gt; not!), it should not be doing it.</font><br> <p> You've just killed all compiler optimization.<br> <p> <font class="QuotedText">&gt; Isn't it common sense that any code compiled today _can_ be used in a </font><br> <font class="QuotedText">&gt; multi-threaded environment?</font><br> <p> And any variable might be shared. So now the compiler must assume everything is volatile.<br> <p> You've just killed all compiler optimization.<br> <p> </div> Wed, 08 Aug 2012 16:56:10 +0000 ACCESS_ONCE() https://lwn.net/Articles/510237/ https://lwn.net/Articles/510237/ daglwn <div class="FormattedComment"> That *is* the --do-what-I-mean-not-what-I-write switch.<br> <p> How can the compiler possibly know what you meant when you wrote something outside the language spec?<br> <p> Take a very simple but common issue: undefined data. There are countless compiler analyses and transformations that mode code around, reallocate stack space, etc. that cause the undefined variable to have different (garbage) values. What might happen to work one day most assuredly will not when compiled with a compiler 2-3 versions newer.<br> <p> What should the compiler do? Disable all code motion of and around the offending expression? That would be lunacy.<br> <p> </div> Wed, 08 Aug 2012 16:51:47 +0000 Declaring it volatile https://lwn.net/Articles/510210/ https://lwn.net/Articles/510210/ PaulMcKenney <div class="FormattedComment"> To each their own, I guess.<br> </div> Wed, 08 Aug 2012 15:26:45 +0000 ACCESS_ONCE() https://lwn.net/Articles/510184/ https://lwn.net/Articles/510184/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; The problem is that the compiler has *no way* to know if the code is potentially harmful</font><br> <p> It's not the code that it's harmful, but the transformation the compiler does in the optimization. To put it another way: the compiler chooses to do something that is NOT what the code says. It does so because it assumes it's safe. Because there's no way it can be sure if the transformation is safe (or not!), it should not be doing it.<br> <p> <font class="QuotedText">&gt; The compiler doesn't know which parts of code will be operating in a multi-threaded environment</font><br> <p> Isn't it common sense that any code compiled today _can_ be used in a multi-threaded environment?<br> <p> </div> Wed, 08 Aug 2012 14:30:29 +0000 ACCESS_ONCE() https://lwn.net/Articles/510183/ https://lwn.net/Articles/510183/ dgm <div class="FormattedComment"> <font class="QuotedText">&gt; The famous --do-what-I-mean-not-what-I-write compiler switch.</font><br> <p> That would be a good switch, yes. By default the compiler should be in --do-what-I-write-not-what-some-ambiguous-line-of-the-spec-allows-you-to-interpret, though. It would be better for everybody.<br> </div> Wed, 08 Aug 2012 13:31:01 +0000 Declaring it volatile https://lwn.net/Articles/510055/ https://lwn.net/Articles/510055/ PaulMcKenney <div class="FormattedComment"> Here is the URL you are looking for: <a href="http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html">http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html</a><br> </div> Tue, 07 Aug 2012 21:26:33 +0000 Declaring it volatile https://lwn.net/Articles/510054/ https://lwn.net/Articles/510054/ paulj <div class="FormattedComment"> I have to ask, what is this ({…}) GCCism? I've searched the manual and online for it and "variably modified type" but not found anything useful.<br> </div> Tue, 07 Aug 2012 21:12:31 +0000 Declaring it volatile https://lwn.net/Articles/510050/ https://lwn.net/Articles/510050/ daglwn <div class="FormattedComment"> If the programmmer is optimizing by using a temporary, the assumption is that the programmer knows what he or she is doing. The optimization is explicit and is an immediate flag to the reader to carefully examine the (existing or new) code for bugs.<br> <p> I'm not trying to convince you, simply stating what I believe is good programming practice. Maybe I'm wrong. But it's worked for me so far. :)<br> </div> Tue, 07 Aug 2012 19:44:50 +0000 ACCESS_ONCE() https://lwn.net/Articles/510049/ https://lwn.net/Articles/510049/ daglwn <div class="FormattedComment"> Oh, we have plenty of cases where we try to cater to users who stretch (to put it mildly) the standard. But one must recognize that the programmer is no longer programming in said language. He or she is programming in some other, ill-defined language.<br> <p> My point is that a bug caused by the programmer not adhering to the language standards is not caused by the compiler. The compiler team may be willing to work around the problem but that doesn't mean the code ain't broke.<br> <p> <font class="QuotedText">&gt; If something has the potential of doing harm, and the user is perfectly </font><br> <font class="QuotedText">&gt; capable of doing it by hand anyway if really needed, then it's best for </font><br> <font class="QuotedText">&gt; the compiler not to do it.</font><br> <p> The problem is that the compiler has *no way* to know if the code is potentially harmful because the code is doing something outside the definition of the language. The problem ACCESS_ONCE is trying to solve is a perfect example. The compiler doesn't know which parts of code will be operating in a multi-threaded environment, much less which pieces of memory are shared. We have "volatile" to mark variables in a way that happens to work for our current threading models. But it is the only thing we have in C right now.<br> <p> Compiler writers generally don't want to limit legal transformations because those transformations can help the vast majority of programmers who don't have whatever specialized problem one programmer might complain about.<br> <p> <p> </div> Tue, 07 Aug 2012 19:42:16 +0000