简体   繁体   English

Delphi是否有任何与C的volatile变量等价的东西?

[英]Does Delphi have any equivalent to C's volatile variable?

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. 在C和C ++中,变量可以标记为volatile ,这意味着编译器不会对其进行优化,因为它可以在声明对象外部进行修改。 Is there an equivalent in Delphi programming? 在Delphi编程中是否有相同的东西? 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. 我的想法是使用绝对 ,但我不确定,这可能会引入其他副作用。

Short answer: no. 简答:不。

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. 同样,限制写入单个分配。

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. 当在表达式之间调用非内联方法时,Delphi编译器不对非本地位置表达式执行公共子表达式消除(CSE),因为编译器不进行过程间优化,因此即使对于单个进程也不正确 - 线程代码。

So, you may want to use InterlockedExchange() to do your reads and writes to force this; 因此,您可能希望使用InterlockedExchange()来执行读取和写入操作; additionally, this will cause a full memory barrier, so the processor won't reorder reads and writes either. 此外,这将导致完整的内存屏障,因此处理器也不会重新排序读取和写入。

According to The Delphi Language for Mobile Development whitepaper, Delphi's mobile compilers have supported a [volatile] attribute since they were first introduced: 根据Delphi移动开发语言白皮书,Delphi的移动编译器自首次引入以来支持[volatile]属性:

The volatile attribute is used to mark fields that are subject to change by different threads, so that code generation does not optimize copying the value in a register or another temporary memory location. volatile属性用于标记可能由不同线程更改的字段,因此代码生成不会优化复制寄存器或其他临时内存位置中的值。

You can use the volatile attribute to mark the following declarations: 您可以使用volatile属性标记以下声明:

  • Variables (global and local) 变量(全局和本地)
  • Parameters 参数
  • Fields of a record or a class. 记录或类的字段。

You cannot use the volatile attribute to mark the following declarations: 您不能使用volatile属性标记以下声明:

  • Type 类型
  • Procedures, Functions or Methods 程序,功能或方法
  • Expressions 表达式

 type TMyClass = class private [volatile] FMyVariable: TMyType; end; 

Starting with Delphi 10.1 Berlin, the desktop compilers now support [volatile] as well. 从Delphi 10.1 Berlin开始,桌面编译器现在也支持[volatile]

Attributes Supported by All Compilers 所有编译器支持的属性

Now, all Delphi compilers support the following attributes: 现在,所有Delphi编译器都支持以下属性:

I don't know of any equivalent, nor do I think that the absolute directive will help you. 我不知道任何等价物,也不认为绝对指令会对你有帮助。 absolute allows you to have two variables that use the same address, but I do not think it will prevent the compiler from optimising references to that memory. absolute允许你有两个使用相同地址的变量,但我认为它不会阻止编译器优化对该内存的引用。

I imagine you could use a pointer and manage it yourself. 我想你可以使用指针并自己管理它。 That way whatever the compiler does as far as optimising retrival of the pointer value, it should not assume the value stored at the address is the same as last time it read it, but this is pure speculation. 无论编译器如何优化指针值的优化,它都不应该假设存储在地址中的值与上次读取时的值相同,但这是纯粹的推测。

Delphi for .Net does not have the keyword either, but the .Net platform has util functions for it. Delphi for .Net也没有关键字,但.Net平台具有util功能。 See Thread.VolatileRead and Thread.VolatileWrite . 请参见Thread.VolatileReadThread.VolatileWrite

Use dynamically allocated pointers? 使用动态分配的指针?

var
  MyVarPtr: ^integer;
begin
  New(MyVarPtr);
  MyVarPtr^ := 5;
...

This should keep the compiler from using a register for the integer value (but it might still use one for the address). 这应该使编译器不使用寄存器来获取整数值(但它仍然可以使用一个用于地址)。 I am not sure how that compares to volatile, though. 不过,我不确定这与挥发性有何不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM