from here it says
"The ThreadVar keyword starts a set of variable definitions that are used by threads. Each thread is given a separate instance of each variable, thereby avoiding data conflicts, and preserving thread independence. "
So can I use in Parallel.For like this?
threadvar
threadID: integer;
procedure TForm5.Button1Click(Sender: TObject);
var
Tot: Integer;
begin
TParallel.For(1, Max, procedure (I: Integer)
begin
threadID := i; // each thread gets its own threadID?
if IsPrime (threadID) then
TInterlocked.Increment (Tot);
end);
end;
You can certainly use threadvar
with PPL code. Internally the PPL code stands on top of the system threading libraries, and so threadvar
works as you would expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With