Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-06

Multiple Threads created from the same TThread object

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Malcom Haak

Hi all,

I am having a bit of trouble with my multi-threaded application. Basically I have a TStringList and for each string in the list I create a TThread object and assign it to the object entry for that string.

My issue is it seems that the First instance of this thread works where as all instances after the first do not appear to work correctly. The method only uses variables that are local to the method, and all other variables are actually added to the Thread class. So they should be created new each time a new thread is created.

Is this the correct way to do this, or am I missing something?

Regards

Malcolm Haak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110627/40df439c/attachment.html>

Reply by email

Michael Schnell
to Malcom Haak
Malcom Haak wrote 6 lines
My issue is it seems that the First instance of this thread works where as all instances after the first do not appear to work correctly. The method only uses variables that are local to the method, and all other variables are actually added to the Thread class. So they should be created new each time a new thread is created.

It should be no problem to instantiate the same TThread sibling multiple times (e.g. using myThread[i] := TmyThread.Create() ). In most projects the instance needs to know which one it is, so it might be sensible to have a property TmyThread.Instance and assign the instance number before the thread runs (or e.g. use Tag):

myThread[i] := TmyThread.Create(True);
myThread[i].Tag := i;
myThread.Suspended := False;

(or similar)

-Michael

Reply by email