[Lazarus] Threads

Mattias Gaertner nc-gaertnma at netcologne.de
Thu Mar 22 16:20:17 CET 2012


Antonio Fortuny <a.fortuny at sitasoftware.lu> hat am 22. März 2012 um 15:32
geschrieben:

> Hi folks.
>
> Wen launching a thread, the process responsible for the thread object
> creation and the call to TThread.Start and the thread EXECUTE procedure,
> run in parallel. That's why the SYNCHRONIZE procedure exists and that
> the developer has to pay attention to not share code between threads.
> So far, so good.
> Now imagine that the EXECUTE procedure of a thread creates a useful
> object (TMyObject) with properties and methods. This thread is launched
> half a dozen times to process half a dozen simultaneous operations.
> My question is:
> How the properties and methods of the TMyObject created in every
> launched thread are they managed from one thread to another. As far as
> the data segment is concerned, no problem, I guess that every instance
> of the TMyObject has its own data in its thread address space: no mix.
> But what about the code and the local methods stack: will it be shared ?
> And if answer is yes, to what should I take care ?


Each thread has its own stack.
All threads share the same address space and the same heap.
Objects (here: class instances) are created on the heap. The heap is thread
safe.
The data segment is for constants. They are shared.



>
> The question is far from trivial: I try to keep the EXECUTE procedure as
> small as possible and spread the code and data over a number of
> specialized units avoiding to load the EXECUTE procedure stack with
> heavy data ans structures.
>
> I apologize if my question doesn't sound very clear, but it's not even
> clear in my mind :-[



Mattias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20120322/8c0c3d25/attachment-0003.html>


More information about the Lazarus mailing list