德尔福是否有任何相当于C中的volatile变量? [英] Does Delphi have any equivalent to C's volatile variable?

查看:295
本文介绍了德尔福是否有任何相当于C中的volatile变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C和C ++的变量可以被标记为 挥发性 ,这意味着编译器不会优化它,因为它可被修改外部的声明对象。是否有Delphi程序等同?如果不是关键字,也许周围的工作?

In C and C++ a variable can be marked as volatile, which means the compiler will not optimize it because it may be modified external to the declaring object. Is there an equivalent in Delphi programming? If not a keyword, maybe a work around?

我的想法是使用绝对,但我不知道,这可能会引入其他副作用。

My thought was to use Absolute, but I wasn't sure, and that may introduce other side effects.

推荐答案

简短的回答:没有

不过,我不知道其中的编译器保守的方法将改变数的读取或写入,如果你按照这个办法的情况:

However, I am not aware of any situation in which the conservative approach of the compiler will change the number of reads or writes if you follow this approach:

在读一跨线程显眼的位置,它的值保存到本地做任何进一步处理之前;同样,限制写入分配一次。

When reading a cross-thread visible location, save its value to a local before doing any further manipulation; similarly, restrict writes to a single assignment.

在Delphi编译器不执行常见SUBEX pression消除(CSE)对当有到EX pressions之间的非内联函数调用的非本地位置前pressions,因为编译器没有按做T间优化,因此它不会是正确的,即使对于单线程code。

The Delphi compiler does not perform common subexpression elimination (CSE) on non-local location expressions when there are calls to non-inlined methods between the expressions, as the compiler doesn't do interprocedural optimization and thus it would not be correct even for single-threaded code.

所以,你可能需要使用InterlockedExchange()做你的读取和写入强制这一点;此外,这将导致一个完整的内存屏障,因此处理器不会重新排序的读取和写入任何

So, you may want to use InterlockedExchange() to do your reads and writes to force this; additionally, this will cause a full memory barrier, so the processor won't reorder reads and writes either.

这篇关于德尔福是否有任何相当于C中的volatile变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆