[Lazarus] A few questions on threads

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Tue Oct 4 11:43:24 CEST 2011



On Tue, 4 Oct 2011, Frank Church wrote:

> What is the purpose of the while not Terminated in a threads execute loop?
> If a thread is doing its own thing and knows when or not it has finished
> what is the purpose of the while not Terminated
>
> Is Terminated designed to be called by other procedures besides the thread
> itself? Can a thread be Terminated externally?

Yes.

> This also seems to imply that if the code within the while not Terminated
> loop is a long running sequence a Termniate command will not be processed
> until control returns to the beginning of the loop

The Execute method is supposed to check Terminated at regular intervals.

>
> Can a thread respond to a new Execute command while it already Executing? Do
> threads have ways of being interrogated about their state while Executing?

No. It is an error to call execute twice.

The only interrogation you can do is check for 'Terminated'.


> Suspend and Resume have been deprecated, and I want to know how a thread can
> be suspended.

There is - to my knowledge - no safe way of doing so.

> If a thread is not set to FreeOnTerminate, can a thread suspend itself by
> using Terminate or some other custom function, doing some clean ups and
> waiting so it can be restarted by the Start procedure, by adding some custom
> fields to make it itself aware that it is an a paused state, so that when an
> Execute is sent it look up those fields, set Terminated to false and
> continue as though nothing has happened?

Terminate should only be used to actually terminate the thread.
You can implement some custom commands to put them in a 'suspended' state,
if you want. (using mutexes, semaphores and whatnot)

You should assume only that if Execute() exits, the thread will be
cleaned up.

Michael.




More information about the Lazarus mailing list