[fpc-pascal] Call function in shared library from multiple threads

Michael Van Canneyt michael at freepascal.org
Fri Mar 31 15:04:31 CEST 2017

On Thu, 30 Mar 2017, Michael Van Canneyt wrote:

> On Thu, 30 Mar 2017, Krzysztof wrote:
>> 2017-03-30 15:19 GMT+02:00 Michael Van Canneyt <michael at freepascal.org>
>>> Nevertheless, that should work.
>>> I've used it in multi-threaded apache modules.
>> Well it doesn't work or fpjson module is not thread safe in this case. You
>> can test it by yourself with attached demo (first post). Try it few times
>> because sometimes if you are lucky then there is no error
> If I find a moment, I will test.

I can reproduce the problem.

At least partially it is related to the threads mechanism not being initialized

To fix that, you can do the following. 
In the library startup code, create a dummy thread. 
This will initialize the threads mechanism:

library testlib;

{$mode objfpc}{$H+}

   cthreads, cmem, Classes, testlibu;

   InitTest, FinalizeTest, Test;

   with TThread.Create(False) do


After this, I no longer get the problem (which doesn't mean there are no
other possible problems :) )


More information about the fpc-pascal mailing list