• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

10.3.4 Threads and dynamic predicates
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Multithreaded applications
        • Thread communication
          • Threads and dynamic predicates
            • thread_local/1
    • Packages
Availability:built-in
Sourcethread_local +Functor/+Arity, ...
This directive is related to the dynamic/1 directive. It tells the system that the predicate may be modified using assert/1, retract/1, etc., during execution of the program. Unlike normal shared dynamic data, however, each thread has its own clause list for the predicate. As a thread starts, this clause list is empty. If there are still clauses when the thread terminates, these are automatically reclaimed by the system (see also volatile/1). The thread_local property implies the properties dynamic and volatile.

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.

ClioPatria (version V3.1.1-51-ga0b30a5)