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

Dominique Leducq dleducq at magellan-ing.fr
Thu Nov 30 10:53:37 CET 2006


On Thu, 30 Nov 2006 10:11:17 +0100 (CET)
Michael Van Canneyt <michael.vancanneyt at wisa.be> wrote:

> 
> 
> 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
> enough.

I use Fpc 2.1.1 (currently rev 5448), and something still seems to be needed. In which revision have your fixes been applied ?


> 
> 
> > 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.
> 
> Michael.

Thanks Michael for your (very) fast answer !

Dominique




More information about the fpc-devel mailing list