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

5

14

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.

Palette answered 24/10, 2008 at 0:16 Comment(5)
Absolute introduces an alias, a different name (and possibly type) for the same location.Doyen
If I may ask, why is this needed? Maybe there is another way to do what you need.Buchbinder
A co-worker was just curious. I asked him the same thing (why). I told him what I thought, but that I knew how to find out.Palette
Your description of what volatile means is imprecise at best.Persevere
@DavidHeffernan I over simplified my definition. Feel free to elaborate or correct it.Palette
D
15

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.

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.

Doyen answered 24/10, 2008 at 1:27 Comment(0)
N
14

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:

Nagaland answered 10/5, 2016 at 17:37 Comment(0)
E
1

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.

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.

Er answered 24/10, 2008 at 1:3 Comment(0)
B
1

Delphi for .Net does not have the keyword either, but the .Net platform has util functions for it. See Thread.VolatileRead and Thread.VolatileWrite.

Beggarweed answered 24/10, 2008 at 8:1 Comment(0)
I
0

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.

Inheritance answered 24/10, 2008 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.