# Engines and thread safety when using engine\_next\_reified

**URL:** <https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857>\
**Category:** General\
**Created:** [March 6, 2025, 1:15pm UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857 "2025-03-06T13:15:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [March 6, 2025, 1:15pm UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/1 "2025-03-06T13:15:01Z")

</div>

Given the following sample code:

```prolog
test :-
  engine_create(_, runloop, E),
  forall(between(0,100,_), thread_create(worker(E), _, [detached(true)])).

runloop :- engine_fetch(test), engine_yield(ok), runloop.

worker(E) :- engine_post(E, test), engine_next_reified(E, Result), assertion(Result = the(ok)), worker(E).

```

Very soon almost all threads die with `Thread running "worker(<engine>(3,0x600001630510))" died on exception: engine_post/2: No permission to post_to engine `(3,0x600001630510)'`

Now when I change the two step `engine_post/engine_next` to `engine_post/3`, this seems to work, the access to the engine seems synchronized?

`worker_b(E) :- engine_post(E, test, X), assertion(X == ok), worker_b(E).`

I was under the impression that thread issuing first `engine_post/2` would also obtain some kind of lock and others would wait. In any case, is there a way to get `engine_post/3` behaviour but get the “reified” result, perhaps `engine_post_reified/3`?

---

<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:** [March 6, 2025, 1:48pm UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/2 "2025-03-06T13:48:00Z")

</div>

Nobody says that the thread calling next must be the same as the one that did the post, so I guess this is to be expected. Would be nice if the docs are more explicit about what is safe and what not though …

> [@esad](#):
>
> engine\_post\_reified/3

Not sure about the general usability. engine\_next\_reified/2 is defined in Prolog on top of engine\_next/2, so you can do the same for an `engine_post_reified/3`

---

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [March 6, 2025, 2:11pm UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/3 "2025-03-06T14:11:06Z")

</div>

Thanks for the hint. But is there something special about engine\_post/3 that makes it thread-safe (in my scenario, meaning that post and next happen in a “transaction”)? Or is it just a side effect of the implementation?

---

<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:** [March 6, 2025, 2:42pm UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/4 "2025-03-06T14:42:02Z")

</div>

Post and next do need to lock the engine and engine\_post/3 does not release it in between.

---

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [March 7, 2025, 9:39am UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/5 "2025-03-07T09:39:48Z")

</div>

How would I reproduce this behaviour using just engine\_post/2 and engine\_next, for example doing something in between the two while still having the engine locked for other threads’ engine\_posts? Is there a simple way to “lock” engine? I guess creating a mutex and then using something like blackboard to associate the mutex, or even using blackboard itself to coordinate engine access would work…

---

<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:** [March 7, 2025, 9:56am UTC](https://swi-prolog.discourse.group/t/engines-and-thread-safety-when-using-engine-next-reified/8857/6 "2025-03-07T09:56:47Z")

</div>

A simple binary predicate that associates engines with mutexes and using these mutexes should work. Why would you do something in between though? Engines do not run concurrently. Instead, engine\_next/2 temporarily replaces the current thread engine with the engine on which you do the next. When the answer is ready, the active engine is swapped back. In other words, an _engine_ is a complete Prolog prover (stacks, virtual machine instance, etc.). A Prolog thread is an OS thread that is running an engine. SWI-Prolog allows engines to hop between OS threads (a bit like Pythons (\<3.13) GIL, except that classical Python has only one engine and SWI-Prolog can handle many engines). Engines can also not be associated with any OS thread, which implies they cannot make progress.
