E2189 Thread local variables cannot be local to a function (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

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.