Sign in Join

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

Mailing lists / lazarus / 2011-10

A few questions on threads

18 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Frank Church

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?
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

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?

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

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?

Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111004/74ca3777/attachment.html>

Reply by email

Unknown author
to Frank Church
Frank Church wrote 6 lines
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.

Show quoted text 6 lines
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.

Reply by email

Andreas Berger
to Frank Church: What is the purpose of the while not Terminated in a threads execute loop? If a thread is…
Frank Church wrote 5 lines
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

Your thread is busy chewing up processor time and plans to do this until eternity. Now the user has different plans and decides to close your application. The problem is that the application can not terminate while your thread is chewing up processor time so your application says: "Hey thread, please terminate".

If all of your *while* statements have a not(Terminated) and your *until* statements include a Terminated then your thread will stop and your application will shut down like the user will be happy. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111004/4da95cc7/attachment.html>

Reply by email

Michael Schnell
to Frank Church: What is the purpose of the while not Terminated in a threads execute loop? If a thread is…
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

In Lazarus (and Delphi), "Worker Threads" can't have an "Event Loop" and so "event driven programming" (using "On..." procedures for the user code) is not possible. That is why in a Thread the user needs to create the "Loop" himself. Such a Thread should always run to a stall in some blocking library functions, lest it will take 100 % CPU time.

Show quoted text 6 lines
Is Terminated designed to be called by other procedures besides the thread itself? Can a thread be Terminated externally? 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

A thread should terminate itself by exiting the Execute procedure. If not "FreeOnTerminate" is set. after termination the main thread should free the TThread object.

Can a thread respond to a new Execute command while it already Executing?

No.

Do threads have ways of being interrogated about their state while Executing?

There are lots of ways of doing inter-thread communication. Simplest: any (no "ThreadVar") variable can mutually be read and written. A good (portable) way of notifying the Main thread about a state change is "Application.QueueAsyncCall". "TThread.Synchronize" calls a main thread event while stalling the thread for some indefinite amount of time.

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

Using Semaphores like "TCriticalSection".

Show quoted text 8 lines
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?

In certain applications (if it is not supposed to stall and wait) the main thread needs to check multiple times whether a TThread instance in fact is "Terminated" and thus can be freed.

-Michael

Reply by email

Frank Church
to Unknown author: Yes. The Execute method is supposed to check Terminated at regular intervals. No. It is…
Show quoted text 34 lines
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'.

Isn't it possible to have add new fields to your thread that can be interrogated?
e.g. another routine wants to call the thread's Execute method. Whenever the thread is executing it sets a field named FProcessing:= true, so the routing which wants to call the thread's execute method can read FProcessing and not call the execute until FProcessing is false.
Is this possible?

Show quoted text 23 lines
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)

ie this means that a routine that wants to the thread to stop can call Terminate, but it is up to the Thread itself to respond, it can't be forced?

Show quoted text 11 lines
You should assume only that if Execute() exits, the thread will be cleaned up. Michael. -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111004/a44fb085/attachment.html>

Reply by email

Frank Church
to Michael Schnell: In Lazarus (and Delphi), "Worker Threads" can't have an "Event Loop" and so "event driven…
Michael Schnell wrote 39 lines
On 10/04/2011 11:37 AM, 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 > In Lazarus (and Delphi), "Worker Threads" can't have an "Event Loop" and so "event driven programming" (using "On..." procedures for the user code) is not possible. That is why in a Thread the user needs to create the "Loop" himself. Such a Thread should always run to a stall in some blocking library functions, lest it will take 100 % CPU time. > Is Terminated designed to be called by other procedures besides the thread > itself? Can a thread be Terminated externally? > 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 > A thread should terminate itself by exiting the Execute procedure. If not "FreeOnTerminate" is set. after termination the main thread should free the TThread object. > Can a thread respond to a new Execute command while it already Executing? > No. Do threads have ways of being interrogated about their state while > Executing? > There are lots of ways of doing inter-thread communication. Simplest: any (no "ThreadVar") variable can mutually be read and written. A good (portable) way of notifying the Main thread about a state change is "Application.QueueAsyncCall". "TThread.Synchronize" calls a main thread event while stalling the thread for some indefinite amount of time.

I am and the only parameter it takes
in addition to the method pointer is a PrtInt. That seems to limit the options passed compared with TThread.Synchronize. Can the PtrInt be cast to a pointer to a richer data structure?

Show quoted text 27 lines
> Suspend and Resume have been deprecated, and I want to know how a thread > can be suspended. > Using Semaphores like "TCriticalSection". > 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? > > In certain applications (if it is not supposed to stall and wait) the main thread needs to check multiple times whether a TThread instance in fact is "Terminated" and thus can be freed. -Michael -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111004/1625073e/attachment.html>

Reply by email

Michael Schnell
to Frank Church: Isn't it possible to have add new fields to your thread that can be interrogated? e.g.…
Frank Church wrote
Isn't it possible to have add new fields to your thread that can be interrogated?

Of course you can add any variables and properties to your TMyThread Object. (This is just data-handling and completely independent of the Thread "context" the code is running in.)

e.g. another routine wants to call the thread's Execute method.

This is forbidden. "Execute" runs in the Thread context and is (automatically) called just once per thread Object instance when the thread (instance) is started (automatically when created or by explicitly by "resume").

-Michael

Reply by email

Michael Schnell
to Frank Church: I am and the only parameter it takes in addition to the method pointer is a PrtInt. That…
Frank Church wrote 5 lines
I am and the only parameter it takes in addition to the method pointer is a PrtInt. That seems to limit the options passed compared with TThread.Synchronize. Can the PtrInt be cast to a pointer to a richer data structure?

TThread.Synchronize allows for no QueueAsyncCall is less limited.

The parameter of course can be a pointer to a record or an object that might hold any information.

But you need to be aware that, while the pointer itself is "safe" (i.e. it does not change when arriving at the main thread even if the sending thread changes the value of the variable it gave to QueueAsyncCall), this of course is not true for the information the pointer points to. Here you might need to create a copy (by "New()" or ".Create") if the threads goes on writing to the original and you need to see a consistent object in the main thread. (beware of Strings doing reference counting here!)

"Synchronize" stalls the thread while doing the AsyncCall, so this problem does not arise.

-Michael

Reply by email

Lukasz Sokol
to Unknown author: Yes. The Execute method is supposed to check Terminated at regular intervals. No. It is…
Show quoted text 7 lines
> 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.

The thread could start spinning/sleeping on a /safe/ place in its code ? But that'll require another variable to tell it to do that ?

(or: 2 variables of type Set of (tsRun, tsPause, tsTerminate) (and (tsRunning, tsPaused, tsTerminating) ?) ) - one for signalling to the thread and the other to signal its current state to the world...?

L.

Michael.

Reply by email

Mark Morgan Lloyd
to Unknown author: Yes. The Execute method is supposed to check Terminated at regular intervals. No. It is…
Show quoted text 5 lines
> 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.

Can a thread safely suspend itself, subject to an external resume? If not, what is the "official" alternative?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Unknown author
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 10 lines
wrote: >> 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. Can a thread safely suspend itself, subject to an external resume? If not, what is the "official" alternative?

A thread can always suspend itself. It's the suspend by another thread that is dangerous.

Michael.

Reply by email

Mark Morgan Lloyd
to Unknown author: A thread can always suspend itself. It's the suspend by another thread that is dangerous.…
Show quoted text 15 lines
On Tue, 4 Oct 2011, Mark Morgan Lloyd wrote: > wrote: > >>> 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. > > Can a thread safely suspend itself, subject to an external resume? If > not, what is the "official" alternative? A thread can always suspend itself. It's the suspend by another thread that is dangerous.

Thanks for that Michael. I've got a thread which runs for a stretch in the background to query a database (to avoid a UI glitch), then suspends itself until it's kicked back into life by the main thread approx 30 seconds later. Appears reliable on all tested platforms.

Put another way, I can see the argument for restricting access to Suspend but I hope nobody has got plans to eliminate it completely.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Lukasz Sokol
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 24 lines
wrote: > On Tue, 4 Oct 2011, Mark Morgan Lloyd wrote: > >> wrote: >> >>>> 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. >> >> Can a thread safely suspend itself, subject to an external >> resume? If not, what is the "official" alternative? > > A thread can always suspend itself. It's the suspend by another > thread that is dangerous. Thanks for that Michael. I've got a thread which runs for a stretch in the background to query a database (to avoid a UI glitch), then suspends itself until it's kicked back into life by the main thread approx 30 seconds later. Appears reliable on all tested platforms. Put another way, I can see the argument for restricting access to Suspend but I hope nobody has got plans to eliminate it completely.

I think it'll do just fine to put Suspend into strict private section so no outside module can call it...

L.

Reply by email

Luca Olivetti
to Mark Morgan Lloyd: Thanks for that Michael. I've got a thread which runs for a stretch in the background to…

Al 04/10/2011 15:24, En/na Mark Morgan Lloyd ha escrit:

Show quoted text 5 lines
Thanks for that Michael. I've got a thread which runs for a stretch in the background to query a database (to avoid a UI glitch), then suspends itself until it's kicked back into life by the main thread approx 30 seconds later. Appears reliable on all tested platforms.

Wait on a TEvent instead of suspending. Then provide a method that signals the TEvent, to be called by the main thread (and signal it also in the destructor, so execute can wake up and properly terminate).

Bye

Luca Olivetti Wetron Automation Technology http://www.wetron.es Tel. +34 935883004 (Ext.133) Fax +34 935883007

Reply by email

Frank Church
to Frank Church: What is the purpose of the while not Terminated in a threads execute loop? If a thread is…
Frank Church wrote 29 lines
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? 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 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? Suspend and Resume have been deprecated, and I want to know how a thread can be suspended. 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? -- Frank Church

Do the Start and Resume methods automatically call the Execute method?

======================= http://devblog.brahmancreations.com
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111004/ece8bc59/attachment-0001.html>

Reply by email

Lukasz Sokol
to Frank Church
Frank Church wrote 7 lines
Do the Start and Resume methods automatically call the Execute method?

Clases.inc: line 115

tthread.inc: 98

func.inc: 143 < this is WinAPI call

thread.inc: 143

threadh.inc: 31

:)

Lukasz

Reply by email