| Did you know ... | Search Documentation: |
| Threads and dynamic predicates |
Besides queues (section 10.3.1) threads can share and exchange data using dynamic predicates. The multithreaded version knows about two types of dynamic predicates. By default, a predicate declared dynamic (see dynamic/1) is shared by all threads. Each thread may assert, retract and run the dynamic predicate. Synchronisation inside Prolog guarantees the consistency of the predicate. Updates are logical: visible clauses are not affected by assert/retract after a query started on the predicate. In many cases primitives from section 10.4 should be used to ensure that application invariants on the predicate are maintained.
Besides shared predicates, dynamic predicates can be declared with the thread_local/1 directive. Such predicates share their attributes, but the clause list is different in each thread.
Thread-local dynamic predicates are intended for maintaining thread-specific state or intermediate results of a computation.
The clauses of a thread-local predicate can only be accessed by the
thread that owns them. If another thread gets hold of a clause reference
to such a clause, e.g., because it was sent in a message, using it, for
example with clause/3, clause_property/2, nth_clause/3
or
erase/1,
raises a permission_error(access, thread_local_clause, Ref).
Calling a continuation (see section
4.9) that refers to such a clause in another thread raises a permission_error(call,
thread_local_clause, Continuation). This allows atoms that only
appear in these clauses to be reclaimed by the thread, without global
atom garbage collection (see section
12.4.2.1), and the thread to reclaim retracted clauses of these
predicates itself, without global clause garbage collection.
It is not recommended to put clauses for a thread-local predicate into a file, as in the example below, because the clause is only visible from the thread that loaded the source file. All other threads start with an empty clause list.
:- thread_local
foo/1.
foo(gnat).
DISCLAIMER Whether or not this declaration is appropriate in the sense of the proper mechanism to reach the goal is still debated. If you have strong feelings in favour or against, please share them in the SWI-Prolog mailing list.