[fpc-devel] calls to Fpc-made library from C not thread-safe

Michael Van Canneyt michael.vancanneyt at wisa.be
Thu Nov 30 10:11:17 CET 2006

On Thu, 30 Nov 2006, Dominique Leducq wrote:

> Hi,
> I have a library compiled with Fpc, used from C/C++ programs.
> When these C programs call functions of the library from different threads, things break.

This is unfortunately true :-)

> As I understand it, the exception stack(s), the standard input/output streams, the threadID 
> and stack checking variables use thread variables, and when the thread is created from C, 
> this variables are not allocated nor initialized.

This is correct.

> As a workaround, I thought I could export a function from the library, to be called in each 
> thread before any other library call, which would call CurrentTM.AllocateThreadVars and 
> System.InitThread.

This should no longer be needed. I implemented some fixes recently which
fixes this. If you're working with an officially released version, then you
have the right solution. Calling CurrentTM.AllocateThreadVars should be

> And symetrically, a function to be called after any other library call in the thread, 
> which flushes the input/output streams and calls CurrentTM.ReleaseThreadVars.

Correct. The CurrentTM.Releasevars and flushing should be done just before your
thread ends. This should be done always, with any version of FPC.


More information about the fpc-devel mailing list