RAD Studio (Common)
ContentsIndex
PreviousUpNext
E2189: Thread local variables cannot be local to a function (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.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!