Delphi Error – E2189 Thread local variables cannot be local to a function

Delphi Compiler Error

E2189 Thread local variables cannot be local to a function

Reason for the Error & Solution

Thread local variables must be declared at a global scope.

program Produce;

  procedure NoTLS;
    threadvar
      x : Integer;
  begin
  end;

begin
end.

A thread variable cannot be declared local to a procedure.

program Solve;

  threadvar
    x : Integer;

  procedure YesTLS;
    var
      localX : Integer;
  begin
  end;

begin
end.

There are two simple alternatives for avoiding this error. First, the threadvar section can be moved to a local scope. Secondly, the threadvar in the procedure could be changed into a normal var section. Note that if compiler hints are turned on, a hint about localX being declared but not used will be emitted.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...