Subject: Re: [SWIPL] Calling Prolog from foreign threads!

From: Jan Wielemaker <[email-masked]>
Date: Thu, 27 Sep 2001 15:52:55 +0200

On Thu, 27 Sep 2001, [email-masked] wrote:

[please wrap long lines!]

>I have been working on a project, where I create threads in Java and call a C
>function passing some parameters within each thread. The C function called was compiled as a library from which Prolog is envoked to make queries on some prolog files. For each Java thread, the same file that contains prolog
>rules is loaded. However, each thread may load different files consisting only of facts, dynamically because threads may process different sets of data (facts).
> 
>I read that any foreign thread can create its own Prolog engine for dealing with 
>calling Prolog from C-code. I would like to use the C   interface of SWI-Prolog
>within the C library function I make a call to in Java threads using JNI because my code is mostly C  . My question is "Can I create independent Prolog engines 
>from my C library function that runs within the scope of a Java thread to pose
>queries on possibly different sets of dynamically loaded Prolog files containing only facts?" Since all the instants of Prolog engines will be run on separate foreign threads, I would also have to load the same Prolog file that contains rules to process facts. And, this would create some redundancy, which is not so much of a problem considering that the number of threads will not be a great number and 
>that the rules file is not very big.
> 
>What are the drawbacks of this approach I plan to implement using foreign threads? And, is there any other way of accomplishing this task more efficiently and effectively (Note that if I were to use Prolog threads, then I would have to find a away to make the facts-base of each thread independent from each other. Otherwise, threads would use a shared factsbase, whihc is not acceptable for me)?
> 
>I would also be greatful if someone could e-mail me example code for using multithreading support of SWI-Prolog in C   code using native C-thread calls.

Using Prolog threads should work, provided you use native threads in
Java, so each java thread is a native thread and the same native
thread can have a Prolog engine.

Now, simply call PL_thread_attach_engine(NULL) in a thread before doing
anything else involving Prolog from this thread.

You can load your different knowledge bases into different modules, or
tag the data with the thread-identifier.

If calls don't take long though, I would recomment using
single-threaded Prolog and serialize the calls from the various
Java threads on the same Prolog engine.

	Regards --- Jan