Subject: Re: [SWIPL] Multithreading problem

From: Jan Wielemaker <[email-masked]>
Date: Mon, 10 Dec 2001 10:10:09 +0100

On Mon, 10 Dec 2001, Mihir Malviya wrote:
>
>Hi,
>  I need to embedded SWI-prolog into a c++ application which is
>multithreaded.My query is , do I have to build prolog using the
>multi-threaded option?
>or  can the single threaded version also be embedded into the application?

This is possible, but then you should ensure the engine is called
respecting all synchronisation requirements of the single-threaded
version.  In practice this means you need a mutex that ensures only
one thread is busy preparing, calling and handling query-results.

>My second query is the sequence of calls for running in an multithreaded
>environment...
>ie do I need to call PL_initialize(), and if yes, should it be called before
>or after PL_thread_create() ?

If you choose for using the multi-threaded version you still have to
call PL_initialise() before anything else.  Next, a C-thread can decide
to attach an `engine' (set of Prolog stacks that allows for answering
queries) to the thread.  (The thread calling PL_initialise() has an
engine attached right away).  See PL_thread_attach_engine().

What is better depends on your application.  In general I'd go for the
single-threaded version whenever it is feasible to reach a good design
on this basis.  It is just simpler and more robust.  Even if concurrency
is a must (for example because you have multi-processor hardware and
you want to use it) I'd always first check whether using fork() is not
a better option.  In many cases this is the case as it provides good
isolation between the workers which allows the interacting processes
to act appropriately on failing workers.

	Cheers --- Jan