From jan@swi.psy.uva.nl  Mon Jun  5 10:50:42 2000
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id KAA02934;
	Mon, 5 Jun 2000 10:50:41 +0200 (MET DST)
Received: from localhost (localhost [[UNIX: localhost]])
	by gollem.swi.psy.uva.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id KAA27345;
	Mon, 5 Jun 2000 10:51:16 +0200
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Organization: SWI, University of Amsterdam
To: Lalit Jain <lalit@tdiinc.com>, prolog@swi.psy.uva.nl
Subject: Re: Embedded Engine and Multi-threading
Date: Mon, 5 Jun 2000 10:45:28 +0200
X-Mailer: KMail [version 1.0.28]
Content-Type: text/plain
Cc: Lalit Jain <lalit@tdiinc.com>
References: <Pine.GSO.4.21.0005311039460.6196-100000@neptune.tdiinc.com>
In-Reply-To: <Pine.GSO.4.21.0005311039460.6196-100000@neptune.tdiinc.com>
MIME-Version: 1.0
Message-Id: <00060510511607.27098@gollem>
Content-Transfer-Encoding: 8bit

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

