Subject: Re: Embedded Engine and Multi-threading

From: Jan Wielemaker <[email-masked]>
Date: Mon, 5 Jun 2000 10:45:28 +0200

On Wed, 31 May 2000, Lalit Jain wrote:

>I am trying to use SWI-Prolog as an embedded engine in a multi-threaded
>application. I want to create "multiple instances of prolog engine in a
>single process" (for instance, each thread having its own prolog engine).
>As a result, this would allow me to have each prolog engine load its
>own set of rules/facts and execute goals.

In the multi-threaded version, rules and facts are shared.  Each Prolog
engine only has its private stacks.  You could consider using modules
to store rules and facts for a specific thread.

>My question is whether this is supported in the SWI-Prolog. If yes,
>could you please tell how it can be done. If no, do you know of any
>alternative to achieve this.
>
>The SWI-Prolog reference manual says in sec. 5.6.10 on foreign code and
>prolog threads:
>"If you are using SWI-Prolog as an embedded engine in a multi-threaded
>application you can access the Prolog engine from multiple threads by
>creating an engine in each thread from which you call Prolog."
>
>I'm not sure what exactly this means, whether I can create multiple
>engines (one in each thread) or whether I could only create one engine but
>access it from multiple threads by attaching the thread to that engine.
>
>I have tried creating multiple threads and using
>PL_thread_attach_engine() but it seems that only one
>engine instance exists. But I am not sure if I have done the right way.

PL_thread_attach_engine() should be called from a thread started in
foreign code that wants to call Prolog.  If the thread does not yet
have a Prolog engine (= Virtual machine interpreter with stacks), this
will be created.  Multiple C-threads each call their own Prolog engine
that executes the queries in parallel.  Predicates, records, etc. are
visible from all threads.

	Regards --- Jan