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

前端 未结 5 552
走了就别回头了
走了就别回头了 2020-12-16 15:49

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 eq

5条回答
  •  执念已碎
    2020-12-16 16:40

    According to The Delphi Language for Mobile Development whitepaper, Delphi's mobile compilers have supported a [volatile] attribute since they were first introduced:

    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.

    You can use the volatile attribute to mark the following declarations:

    • Variables (global and local)
    • Parameters
    • Fields of a record or a class.

    You cannot use the volatile attribute to mark the following declarations:

    • 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.

    Attributes Supported by All Compilers

    Now, all Delphi compilers support the following attributes:

    • unsafe
    • volatile
    • weak

提交回复
热议问题