TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The ACCESS_ONCE() macro (2012)

13 点作者 gaoprea超过 10 年前

2 条评论

kazinator超过 10 年前
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 未加载
cperciva超过 10 年前
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 未加载