[fpc-devel] Off-topic: Threadvar in C

Michael Schnell mschnell at lumino.de
Fri Aug 1 14:14:54 CEST 2008


Sorry to bother you by asking this here, but I don't know a better place. :(

I need to port a multi-threaded program in ANSI C. From a propriety OS 
to Linux.

Here the same code is started in multiple threads.

The OS provides a global variable that is preloaded with a "thread 
number" of the running thread  (1, 2, 3, 4, .... n). This is something 
like a threadvar and can e.g. be used to create an array of records that 
holds thread specific values.

Now in Linux I  need to use the pthread library. Here I don't find 
informations on how to hold thread specific values. Of course I could 
request the thread id and search a dedicated list of the created threads 
every time I need to access these values, but this would provide a 
_huge_ overhead. I can't believe that this is the way to go. And using a 
normal stack-variable is not practical at all as it would be necessary 
to pass it don the complete function hierarchy. A simple global integer 
preloaded with a predefinable number when the OS schedules a thread 
would be enough.

I suppose in Linux, FP uses the pthread library to create the TThread 
functionality.  As the language provides the "threadvar" keyword, there 
needs to be a decent implementation of this functionality. I hope one of 
the compiler developers knows how this is done and/or where I can find 
this in the source code of the compiler and/or the rtl.

Thanks a lot !

-Michael




More information about the fpc-devel mailing list