Board index » delphi » Threadvar keyword

Threadvar keyword

Does anybody know what is the correct use of threadvar ?
Thanks

D. Fourr

 

Re:Threadvar keyword


Quote
>Does anybody know what is the correct use of threadvar ?

Same as a 'var'. Use it on a global level (not in a procedure/function).
'Var' variables are shared among threads while 'threadvar' variables are
created for each thread anew & automagically.

To clarify:

var
  a: integer;

threadvar
  b: integer;

thread X executes:
  a := 0;
  b := 0;

thread Y executes:
  a := 1;
  b := 1;

in thread X, a is 1 while b is still 0;
in thread Y, a is 1 and b is 1

And better don't use 'threadvar' in interface section, this tend to generate
'Internal Error L1086' (D3) or 'Internal Error L511' (D4).

Primoz

Re:Threadvar keyword


threadvar is - as far as I know - the Delphi term for TLS (Thread Local
Storage) aka a variable thats 'global' but on a per thread basis.

gerald

Other Threads