On 06/03/2016 03:46 PM, Paulo Moura wrote:
> Hi,
>
> I'm wondering where does the maximum number of threads comes from? On
> my Mac OS X system, I can create ~2K threads. Is this limit inherited
> from the operating-system or is it defined somewhere in the
> SWI-Prolog sources? Sorry if I missed the explanation somewhere in
> the documentation.
There is no limit in SWI-Prolog. On my Linux box (64 bit, 32Gb main
mem), 30,000 works fine. Above that I get a memory resource error
exception. Not sure you want that many though. The main resource cost is
the C-stack that gets reserved for the thread. I don't know how well
modern OSes handle really large number of threads.
Note that since two releases we have delimited continuations. These
allow you to make `threads' as well. Unlike system threads however these
run cooperatively on the same stacks and thus can share variables. They
are way cheaper as no C stack needs to be reserved, nor is there a need
for the overall state involved with a thread (stacks, I/O, local
predicates, etc.) I wonder how suitable they would be for simulations.
So, basically the scheduler maintains a database of continuations. It
selects the next task to run and calls the continuation. The task
continues and if it has done its job it calls shift/1 to return control
to the scheduler.
Cheers --- Jan