TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The ACCESS_ONCE() macro (2012)

13 pointsby gaopreaover 10 years ago

2 comments

kazinatorover 10 years ago
It has a confusing name, suggesting that it is intended to prevent multiple accesses. (Contrast with `pthread_once` for instance: a mechanism for once-only initialization.)<p>In fact, the very example given in the article shows that <i>without</i> the ACCESS_ONCE, the compiler moved the access outside of the loop---thereby ensuring that it&#x27;s accessed <i>just</i> once, ironically!!! Whereas by using ACCESS_ONCE, we ensure that it&#x27;s accessed as many times as the loop is iterated, not only once.<p>Basically this just does some kind of volatile access, and so the name should reflect that:<p><pre><code> foo = VOLATILE_READ(bar); </code></pre> Then the number of times it is accessed is implied from the agreement between abstract and actual semantics. In the abstract semantics, and expression is evaluated once for each invocation of whatever encloses it. If an expression is unconditionally evaluated in a loop that iterates 100 times, then it is evaluated 100 times. Hoisting it out of the loop would be an optimization which is forbidden by agreement with the abstract semantics.
评论 #9037186 未加载
cpercivaover 10 years ago
The silly thing here is that this macro is not guaranteed to accomplish anything. The C standard limits how compilers can optimize accesses to <i>volatile objects</i>, but says nothing about accesses to non-volatile objects which are accessed via volatile-qualified pointers.
评论 #9038458 未加载
评论 #9035397 未加载