Re: [Lazarus] A few questions on threads

2011-10-05 Thread Lukasz Sokol
On 04/10/2011 15:02, Frank Church wrote:

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


--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Frank Church
On 4 October 2011 10:37, 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?
> 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
--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Luca Olivetti

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



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

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Lukasz Sokol
On 04/10/2011 14:24, Mark Morgan Lloyd wrote:
> michael.vancann...@wisa.be wrote:
>> On Tue, 4 Oct 2011, Mark Morgan Lloyd wrote:
>> 
>>> michael.vancann...@wisa.be 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.


--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Mark Morgan Lloyd

michael.vancann...@wisa.be wrote:

On Tue, 4 Oct 2011, Mark Morgan Lloyd wrote:


michael.vancann...@wisa.be 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]

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Michael Schnell

On 10/04/2011 01:54 PM, Mark Morgan Lloyd wrote:


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



A semaphore.

-Michael

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread michael . vancanneyt



On Tue, 4 Oct 2011, Lukasz Sokol wrote:


On 04/10/2011 10:43, michael.vancann...@wisa.be 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.



The thread could start spinning/sleeping on a /safe/ place in its code ?


That is possible.

Michael.

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread michael . vancanneyt



On Tue, 4 Oct 2011, Mark Morgan Lloyd wrote:


michael.vancann...@wisa.be 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.

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Mark Morgan Lloyd

michael.vancann...@wisa.be 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?


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Lukasz Sokol
On 04/10/2011 10:43, michael.vancann...@wisa.be 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.
> 

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.


--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Michael Schnell

On 10/04/2011 12:53 PM, Frank Church wrote:


I am looking at Application.QueueAsyncCall 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 parameter at all. So 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

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Michael Schnell

On 10/04/2011 12:26 PM, 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

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Frank Church
On 4 October 2011 11:14, Michael Schnell  wrote:

> 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 looking at Application.QueueAsyncCall 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?


>
>> 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
> Lazarus@lists.lazarus.**freepascal.org
> http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus
>



-- 
Frank Church

===
http://devblog.brahmancreations.com
--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Frank Church
On 4 October 2011 10:43,  wrote:

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



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


> You should assume only that if Execute() exits, the thread will be
> cleaned up.
>
> Michael.
>
> --
> __**_
> Lazarus mailing list
> Lazarus@lists.lazarus.**freepascal.org
> http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus
>



-- 
Frank Church

===
http://devblog.brahmancreations.com
--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Michael Schnell

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.


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
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread Andreas Berger



On 4/10/2011 06:37, 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


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.
--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] A few questions on threads

2011-10-04 Thread michael . vancanneyt



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.

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus