gpt4 book ai didi

C++ 作用域变量重新排序和计时器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:46:21 27 4
gpt4 key购买 nike

我对 scoped_lock 的工作原理感到非常兴奋,并且想知道是否可以完成类似的实现来为特定的执行代码计时

如果说我实现了一个简单的类 scoped_timer,它在构建时启动一个计时器并在删除时停止并报告耗时,那么这个示例代码是否会正确计时

func()
{
//some code
{
scoped_timer a;
//some code that does not include a
}
//some code
}

在实践中,我保证 scoped_time a 在开始时构造,并在超出范围时准确销毁。编译器能否决定以不恰好在作用域末尾破坏代码或在开头构造代码的方式重新排序代码,因为不依赖于 object a? C++ 标准是否有保证?

谢谢

丹尼尔

最佳答案

代码保证按照您的意愿运行。

这个保证在 C++ 中很重要,因为 C++ 不是 functional programming language ,由于 C++ 中的几乎所有函数都可能有副作用(无论是来自当前线程的执行流,还是来自其他线程甚至其他进程,无论数据是否声明为 volatile)。因此,语言规范对完整表达式的顺序做出了保证。

为了将 C++11 标准拼凑起来,有许多子句必须一起考虑。

最重要的条款是§1.9:

§1.9 Program execution [intro.execution]

1 The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. * (<-- the footnote is in the standard itself)

* This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

(文字的粗体是我的。)

该条款提出了与该问题相关的两个重要要求。

  1. 如果一个表达式可能有副作用,它会被评估。在你的例子中,表达式 scoped_timer a; 可能有副作用,所以它会被评估。

  2. ...需要符合要求的实现来模拟(仅)抽象机的可观察行为,如下所述。”,其中“以下”包括第 13 条和第 14 条相同部分:

§1.9.13 Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a single thread (1.10), which induces a partial order among those evaluations. Given any two evaluations A and B, if A is sequenced before B, then the execution of A shall precede the execution of B. If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced. [ Note: The execution of unsequenced evaluations can overlap. —end note ] Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which. [ Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. —end note ]

§1.9.14 Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated. * (<-- the footnote here is not relevant)

因此,您的表达式 scoped_timer a;(这是一个完整的表达式)可能会产生副作用,并且会被求值;因此 a 的值的计算将在 block 中的以下任何语句之前排序。

关于a对象的销毁,比较简单。

§3.7.3.3 If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8.

这清楚地表明在 block 退出之前不会调用析构函数。

ADDENDUM 并确认所有 block 级变量在 block 作用域末尾销毁(并调用它们的析构函数),这里是在 C++ 中11 标准:

§3.7.3.1 Block-scope variables explicitly declared register or not explicitly declared static or extern have automatic storage duration. The storage for these entities lasts until the block in which they are created exits.

§3.7.3.2 [ Note: These variables are initialized and destroyed as described in 6.7. —end note ]

...和上述§6.7:

§6.7.2 Variables with automatic storage duration (3.7.3) are initialized each time their declaration-statement is executed. Variables with automatic storage duration declared in the block are destroyed on exit from the block (6.6).

该 block 定义为一对大括号 {} 之间的所有代码:

§6.3.1 So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.

compound-statement:

{ statement-seq }

statement-seq:

statement

statement-seq statement

A compound statement defines a block scope (3.3).

注意:compount-statement(等)部分需要一段时间才能习惯,但重要的一点是,左大括号 { 和右大括号花括号 } 实际上是指代码中的文字左花括号和右花括号。这是 C++11 标准中 block 范围被定义为花括号之间的语句序列的确切位置。

将各个部分放在一起:因为如上所述,标准说这些实体的存储持续到创建它们的 block 退出并且声明了自动存储持续时间的变量 block 中的对象在退出 block 时被销毁,您可以确信问题中的对象a(以及任何 block 级对象)将持续到 block 结束,并且将被销毁并在 block 退出时调用其析构函数。

关于C++ 作用域变量重新排序和计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com