RT如何使用threadvar定义线程专用变量?最重要的是本人不知道在哪个位置定义!
在execute过程中定义
procedure TbThread.Execute;
threadvar
i:integer;
begin
inherited;
if not Terminated then
begin
try
safeObjs.LockList;
i:=compute(1);//返回 11
assignfile(txt,fpath);
append(txt);
writeln(txt,datetimetostr(now) + ' ' + inttostr(i));///////////////////////////报错了
closefile(txt);
safeObjs.UnLockList;
except
safeObjs.UnLockList;
end;
end;
end;
在execute过程中定义
procedure TbThread.Execute;
threadvar
i:integer;
begin
inherited;
if not Terminated then
begin
try
safeObjs.LockList;
i:=compute(1);//返回 11
assignfile(txt,fpath);
append(txt);
writeln(txt,datetimetostr(now) + ' ' + inttostr(i));///////////////////////////报错了
closefile(txt);
safeObjs.UnLockList;
except
safeObjs.UnLockList;
end;
end;
end;
var
global_variant : Integer;改成threadvar
global_variant : Integer;这个global_variant就会在每个线程中产生一个副本, 互相不干扰.