| Did you know ... | Search Documentation: |
| Predicate thread_local/1 |
thread_local +Functor/+Arity,
...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.