Low-level concurrency/multithreading primitives

Author(s): Manuel Carro.

This module provides basic mechanisms for using concurrency and implementing multi-goal applications. It provides a means for arbitrary goals to be specified to be run in a separate stack set; in that case, they are assigned a goal identifier with which further accesses (e.g., asking for more solutions) to the goal can be made. Additionally, in some architectures, these goals can be assigned an O.S. thread, separate from the one which made the initial call, thus providing concurrency and, in multiprocessors, parallelism capabilities.

Documentation on exports



Calls Goal in a new engine (stack set), possibly using a new thread, and returns a GoalId to designate this new goal henceforth. EngineCreation can be either wait or create; the distinction is not yet meaningful. ThreadCreation can be one of self, wait, or create. In the first case the creating thread is used to execute Goal, and thus it has to wait until its first result or failure. The call will fail if Goal fails, and succeed otherwise. However, the call will always suceed when a remote thread is started. The space and identifiers reclaimed for the thread must be explicitly deallocated by calling eng_release/1. GoalIds are unique in each execution of a Ciao Prolog program.

Meta-predicate with arguments: eng_call(goal,?,?,?).



Similar to eng_call/4, but the thread (if created) and stack areas are automatically released upon success or failure of the goal. No GoalId is provided for further interaction with the goal.

Meta-predicate with arguments: eng_call(goal,?,?).


Performs backtracking on the goal designed by GoalId. A new thread can be used to perform backtracking, according to ThreadCreation (same as in eng_call/4). Fails if the goal is backtracked over by the local thread, and there are no more solutions. Always succeeds if executed by a remote thread. The engine is not automatically released up upon failure: eng_release/1 must be called to that end.



Performs a cut in the execution of the goal GoalId. The next call to eng_backtrack/2 will therefore backtrack all the way and fail.



Cleans up and releases the engine executing the goal designed by GoalId. The engine must be idle, i.e., currently not executing any goal. eng_wait/1 can be used to ensure this.



Waits for the engine executing the goal denoted by GoalId to finish the computation (i.e., it has finished searching for a solution, either with success or failure).



Kills the thread executing GoalId (if any), and frees the memory used up by the stack set. Usually one should wait (eng_wait/1) for a goal, and then release it, but killing the thread explicitly allows recovering from error states. A goal cannot kill itself. This feature should be used with caution, because there are situations where killing a thread might render the system in an unstable state. Threads should cooperate in their killing, but if the killed thread is blocked in a I/O operation, or inside an internal critical region, this cooperation is not possible and the system, although stopped, might very well end up in a incosistent state.


Kills threads and releases stack sets of all active goals, but the one calling eng_killothers. Again, a safety measure. The same cautions as with eng_kill/1 should be taken.



    GoalId is unified with the identifier of the goal within which eng_self/1 is executed. eng_self/1 is deprecated, and eng_goal_id/1 should be used instead.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.



    GoalId is unified with the identifier of the goal within which goal_id/1 is executed. goal_id/1 is deprecated, and eng_goal_id/1 should be used instead.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.



    GoalId is unified with the identifier of the goal within which eng_goal_id/1 is executed.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.



    Prints to standard output the current status of the stack sets.


      Usage 1:

      Usage 2:lock_atom(Atom)

      The semaphore associated to Atom is accessed; if its value is nonzero, it is atomically decremented and the execution of this thread proceeds. Otherwise, the goal waits until a nonzero value is reached. The semaphore is then atomically decremented and the execution of this thread proceeds.


      Usage 1:

      Usage 2:unlock_atom(Atom)

      The semaphore associated to Atom is atomically incremented.

      Usage 1:

      Usage 2:atom_lock_state(Atom,Value)

      Sets the semaphore associated to Atom to Value. This is usually done at the beginning of the execution, but can be executed at any time. If not called, semaphore associated to atoms are by default inited to 1. It should be used with caution: arbitrary use can transform programs using locks in a mess of internal relations. The change of a semaphore value in a place other than the initialization stage of a program is not among the allowed operations as defined by Dijkstra [Dij65,BA82].

      Usage 3:atom_lock_state(Atom,Value)

      Consults the Value of the semaphore associated to Atom. Use sparingly and mainly as a medium to check state correctness. Not among the operations on semaphore by Djikstra.


      The predicate named PredName is made concurrent in the current module at runtime (useful for predicate names generated on-the-fly). This difficults a better compile-time analysis, but in turn offers more flexibility to applications. It is also faster for some applications: if several agents have to share data in a stuctured fashion (e.g., the generator knows and wants to restrict the data generated to a set of other threads), a possibility is to use the same concurrent fact and emply a field within the fact to distinguish the receiver/sender. This can cause many threads to access and wait on the same fact, which in turns can create contention problems. It is much better to create a new concurrent fact and to use that new name as a channel to communicate the different threads. concurrent/1 can either be given a predicate spec in the form Name/Arity, with Name and Arity bound, or to give a value only to Arity, and let the system choose a new, unused Name for the fact.


      Known bugs and planned improvements

      • Available only for Windows 32 environments and for architectures implementing POSIX threads.
      • Some implementation of threads have a limit on the total number of threads that can be created by a process. Thread creation, in this case, just hangs. A better solution is planned for the future.