# How to extend maximum of main thread count over 1G?

**URL:** <https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177>\
**Category:** Help!\
**Created:** [January 19, 2024, 6:20pm UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177 "2024-01-19T18:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gyozo](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@Gyozo](https://swi-prolog.discourse.group/u/Gyozo)\
**Post date:** [January 19, 2024, 6:20pm UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/1 "2024-01-19T18:20:11Z")

</div>

I’m using: SWI-Prolog version (threaded, 64 bits, version 9.0.4)

I have quite lot of predicates, and I need more, but at a certain threshold the thing starts not working for me. (It does not send any message…) It seems I run out of “main threads”…

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/f/f33e4f60695cb80d8300e6b2764be7492861f2b2.jpeg)

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [January 20, 2024, 8:53am UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/2 "2024-01-20T08:53:09Z")

</div>

Presumably you’re reaching the stack size limit. You can confirm by running:

```prolog
current_prolog_flag(stack_limit, Limit).

```

If that is the case, you can increase it:

[https://www.swi-prolog.org/FAQ/StackSizes.html](https://www.swi-prolog.org/FAQ/StackSizes.html)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 20, 2024, 9:14am UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/3 "2024-01-20T09:14:23Z")

</div>

Stack size, being 1G by default is also my impression. The report suggests otherwise though. Surely, the number of clauses is not limited other than by memory. I’ve used SWI-Prolog with enough clauses to fill about 200Gb 🙂

Considering that CPU usage drops to zero, it seems to be wait for something. As all seems to be running in the main thread, try Control-C and if it prompts try `g` to get a backtrace.

---

<div class="post-metadata">

**Author:** ![Gyozo](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@Gyozo](https://swi-prolog.discourse.group/u/Gyozo)\
**Post date:** [January 20, 2024, 1:37pm UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/5 "2024-01-20T13:37:20Z")

</div>

Thank You everyone for the hints with stacksize extension. It helped me in some extent. I use now 500G stack size:

cll:- set\_prolog\_stack(global, limit(500 000 000 000)), set\_prolog\_stack(trail, limit(500 000 000 000)),  
set\_prolog\_stack(local, limit(500 000 000 000)), sudoku.

sudoku:-  
a11(A11),  
a12(A12),A11==A12,  
a13(A13),A13==A11,A13==A12,  
a21(A21),A21==A11,A21==A12,A21==A13,  
a22(A22),A22==A11,A22==A12,A22==A13,A22==A21,  
a23(A23),A23==A11,A23==A12,A23==A13,A23==A21,A23==A22,  
a31(A31),A31==A11,A31==A12,A31==A13,A31==A21,A31==A22,A31==A23, …

and it helped to extend the problem search space from **f32** , to almost the end of **g33** …  
but hier - I think I encountered a different limit… I attach an explanation…

 ![500G](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/5/59920f97964b34313adc02a74c60175011fcc27d.jpeg)

so I am affraid… I have to find another solution… ☹

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 20, 2024, 2:02pm UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/6 "2024-01-20T14:02:03Z")

</div>

> [@anon95304481](#):
>
> Do you have a server object abstraction in [SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual)? Its

No clue what that means. The OP’s problem does not involve threads though.

> [@anon95304481](#):
>
> I guess I can block a server via:

Surely keeping locks for indefinite time can deadlock the system. This lock is (only) used to avoid a race condition when the log stream is opened lazily. Locks should typically not be the first choice. Also in this case, it might have been a better design to have a log thread that writes the file and send messages to this thread 🙂

> [@Gyozo](#):
>
> set\_prolog\_stack(global, limit(500 000 000 000)),

This prints 🙂

```prolog
77 ?- set_prolog_stack(global, limit(500 000 000 000)).
Warning: set_prolog_stack/2: limit(Size) sets the combined limit.
Warning: See https://www.swi-prolog.org/changes/stack-limit.html
true.

```

> [@Gyozo](#):
>
> I have to find another solution… ☹

Library clpfd? See [SWISH -- SWI-Prolog for SHaring](https://swish.swi-prolog.org/example/clpfd_sudoku.pl)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 21, 2024, 9:07am UTC](https://swi-prolog.discourse.group/t/how-to-extend-maximum-of-main-thread-count-over-1g/7177/9 "2024-01-21T09:07:12Z")

</div>

> [@anon95304481](#):
>
> Basically the question is can you address and hence access and modify a server for debugging, administrative and management purposes.

To some extend you can address it. In this case the server is addressed by the port it listens on. See e.g., http\_workers/2.

The “hence” is a bit dubious. The above are barely useful for these purposes. For that there is the libssh based login, after which you can inspect and modify a lot. Note that SWI-Prolog provides safe hot reloading of code, i.e., you can reload source files that are at that moment actively used by the server threads as log as you do not change the predicate signatures (number or meaning of arguments). That means you can add, delete or modify clauses of any predicate. That allows for adding debug/3 statements. Only, all this stuff applies to all servers in the process as you address the (probably shared) code, not a particular server. But, you can debug and modify the code behind an HTTP location, you can add or remove locations, modify authentication, etc. All without restarting the server.
