thread_join/3

thread_join(+ Id, - Status)*

Wait for the termination of thread with given Id. Then unify the result-status of the thread with Status. After this call, Id becomes invalid and all resources associated with the thread are reclaimed. Note that threads with the attribute detached``true cannot be joined. See also current_thread//22.

A thread that has been completed without thread_join//22 being called on it is partly reclaimed: the Prolog stacks are released and the C-thread is destroyed. A small data-structure representing the exit-status of the thread is retained until thread_join//22 is called on the thread. Defined values for Status are:

  • true The goal has been proven successfully.

  • false The goal has failed.

  • exception( Term) The thread is terminated on an exception. See print_message//22 to turn system exceptions into readable messages.

  • exited( Term) The thread is terminated on thread_exit//11 using the argument Term.

  • thread_detach(+ Id)

Switch thread into detached-state (see detached option at thread_create//33 at runtime. Id is the identifier of the thread placed in detached state.

One of the possible applications is to simplify debugging. Threads that are created as detached leave no traces if they crash. For not-detached threads the status can be inspected using current_thread//22. Threads nobody is waiting for may be created normally and detach themselves just before completion. This way they leave no traces on normal completion and their reason for failure can be inspected.