[fpc-devel] OO rewrite - technical questions

Mattias Gärtner nc-gaertnma at netcologne.de
Mon Jul 19 15:02:39 CEST 2010

Zitat von Michael Schnell <mschnell at lumino.de>:

>  On 07/19/2010 12:52 PM, Mattias Gärtner wrote:
>> About converting global variables for threads:
>> What are the advantages/disadvantages of using ThreadVar versus  
>> member variables?
> I don't see what you are trying to compare here.

If I understood the mail thread correct, the goal was to change some  
global variables, so that each thread has its own. This can be  
achieved with Threadvar or one object per instance. I don't know how  
ThreadVar is implemented by the compiler on the various systems.

> Threadvars are addressed by a dedicated register (I suppose a  
> segment register with on X86) that is different for each thread ant  
> points to a memory area that is dedicated to each thread.

Can some compiler guru confirm this?
After a quick glance on the rtl sources it seems ThreadVar works under  
some platforms internally pretty much the same as an object instance:  
it allocates some memory on the heap.

> Member variables are addressed using the self pointer of the object  
> and thus they are located on the heap and different for each  
> instance of the Object but common for all threads.
> I suppose the speed difference is very small but the function is  
> very different.
> If you want an instances of a class for each thread you need to have  
> each thread create it's own instance (on the heap) and and hold the  
> self-Pointer (the "instance"-variable ) in a threadvar.


More information about the fpc-devel mailing list