From jan@swi.psy.uva.nl Thu Sep 27 15:58:53 2001
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f8RDwrv29752;
	Thu, 27 Sep 2001 15:58:53 +0200 (MET DST)
Received: from localhost (localhost [[UNIX: localhost]])
	by gollem.swi.psy.uva.nl (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id f8RDwrP01704;
	Thu, 27 Sep 2001 15:58:53 +0200
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Organization: SWI, University of Amsterdam
To: mdonder@cs.bilkent.edu.tr, prolog@swi.psy.uva.nl
Subject: Re: [SWIPL] Calling Prolog from foreign threads!
Date: Thu, 27 Sep 2001 15:52:55 +0200
X-Mailer: KMail [version 1.0.29.2]
Content-Type: text/plain
References: <200109271015.NAA05033@lists.bilkent.edu.tr>
In-Reply-To: <200109271015.NAA05033@lists.bilkent.edu.tr>
MIME-Version: 1.0
Message-Id: <01092715585303.28162@gollem>
Content-Transfer-Encoding: 8bit

On Thu, 27 Sep 2001, mdonder@cs.bilkent.edu.tr 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  

