# Any good solutions for parallelism?

**URL:** <https://swi-prolog.discourse.group/t/any-good-solutions-for-parallelism/6423>\
**Category:** General\
**Created:** [March 31, 2023, 7:36am UTC](https://swi-prolog.discourse.group/t/any-good-solutions-for-parallelism/6423 "2023-03-31T07:36:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexeyKosarchuk](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/alexeykosarchuk/32/4466_2.png) [@AlexeyKosarchuk](https://swi-prolog.discourse.group/u/AlexeyKosarchuk)\
**Post date:** [March 31, 2023, 7:36am UTC](https://swi-prolog.discourse.group/t/any-good-solutions-for-parallelism/6423/1 "2023-03-31T07:36:03Z")

</div>

Hello. Can you point me in the right direction to look for good solution to my problem.  
I want to paralellize some small tasks. I tried to look into concurrent/2 or /3, but it appears to create new threads upon each call. Thread creating is really expensive operation so it is not good to parallelise small tasks. Is there library than can do the same thing, but use Thread Pools instead to optimize things?

Ideally I’am looking for Prolog equivalent of PLINQ - Parallel LINQ from C# language.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2023, 9:14am UTC](https://swi-prolog.discourse.group/t/any-good-solutions-for-parallelism/6423/2 "2023-03-31T09:14:11Z")

</div>

> [@AlexeyKosarchuk](#):
>
> concurrent/2

I can not find concurrent/2 but do find concurrent/3.

* * *

Did you try concurrent\_forall/3?

Here is an example usage:

> [@Building a port scanner](https://swi-prolog.discourse.group/t/building-a-port-scanner/2580/28):
>
> The next version adds [library(persistency)](https://www.swi-prolog.org/pldoc/man?section=persistency). :- use\_module(library(persistency)). :- working\_directory(\_,'C:\\Users\\Eric\\Documents\\Port Scans'). :- persistent port\_scan\_result(port:integer,result:atom). :- initialization(db\_attach('port\_scan\_result.journal', [])). exists\_port\_scan\_result(Request,Response) :- port\_scan\_result(Request,Response). add\_port\_scan\_result(Request,Response) :- with\_mutex(port\_scan\_result\_journal, assert\_port\_scan\_result(Request,Response)). concurrent…

IIRC concurrent\_forall/3 uses a thread pool, check the code as the documentation may not reflect all of the details.

---

<div class="post-metadata">

**Author:** ![AlexeyKosarchuk](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/alexeykosarchuk/32/4466_2.png) [@AlexeyKosarchuk](https://swi-prolog.discourse.group/u/AlexeyKosarchuk)\
**Post date:** [March 31, 2023, 7:55pm UTC](https://swi-prolog.discourse.group/t/any-good-solutions-for-parallelism/6423/3 "2023-03-31T19:55:35Z")

</div>

Yeah, I’ve looked into the code, it appears to create new threads for each invocation. For my scenario it is too expensive. Maybe I’ll make modified version that uses thread pool.
