Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to create a Thread Safe global TList?

How to create a Thread Safe global TList ?

unit Unit1;
interface
uses
    ...;
type
  TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;
  global_TList: TList; // Not thread safe?

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
// something
end;

end.

I have two threads, which can write to global_TList , but as I know, it's not thread safe.

So how to make it safe?

Delphi 2010, Indy 10, Win7

like image 565
jmp Avatar asked Dec 07 '11 00:12

jmp


1 Answers

Use TThreadList. Problem solved.

like image 180
dthorpe Avatar answered Oct 14 '22 03:10

dthorpe