[Lazarus] Threads

Hans-Peter Diettrich DrDiettrich1 at aol.com
Fri Mar 23 10:59:32 CET 2012

Antonio Fortuny schrieb:

> Trying to be as much clear as possible, there are some sentences:
> Assuming this few statements (aka my own rules when writing thread's code):
> a. all thread code+data are encapsulated into a TThread object
> b. "Thread safe" means that there is no overlap of data references from 
> thread to thread, the main thread inclusive, and that no references are 
> passed outside the thread's control from thread to thread (by the means 
> of the owner or global vars for instance)
> c. the thread creator does not interfere in any way with the thread's 
> data and methods as soon as the thread has been started (Terminated 
> execpted of course)
> d. no function neither procedure calls are made to any function or 
> procedure defined outside of the thread control (aka self defined methods)

Looks good :-)

> Do you all agree on the following asserts:
> 1. All variables in the thread definition (TThread's private, protected 
> and public variables) are "Thread safe" BUT are accessible to the thread 
> creator

The OPL visibility rules apply. Public members are accessible by 
everybody, others only from inside the unit where the thread class is 
declared. As long as the thread creator doesn't export the thread 
object, nobody else can access it, of course.

> 5. all useful code a thread needs should be encapsultated into a TObject 
> descendant and instantiated within the thread's space.

This can all be implemented in the derived TThread class.

> 6. all methods defined in the thread's definition, aprat from the 
> EXECUTE procedure (obvious !), run into the  thread creator space (the 
> one which instantiates the thread, aka does something like wThread := 
> TMyThread.Create (False) )

Every procedure or method runs in the context of its *caller*. 
Everything called from the thread's Execute method executes in the 
thread context (stack, threadvars).


More information about the Lazarus mailing list