thread_at_exit(:Goal)Run Goal just before releasing the thread resources. This is
to be compared to at_halt/1,
but only for the current thread. These hooks are run regardless of why
the execution of the thread has been completed. When these hooks are
run, the return code is already available through
thread_property/2
using the result of thread_self/1
as thread identifier. Note that there are two scenarios for using exit
hooks. Using thread_at_exit/1
is typically used if the thread creates a side-effect that must be
reverted if the thread dies. Another scenario is where the creator of
the thread wants to be informed when the thread ends. That cannot be
guaranteed by means of thread_at_exit/1
because it is possible that the thread cannot be created or dies almost
instantly due to a signal or resource error. The at_exit(Goal)
option of
thread_create/3
is designed to deal with this scenario.
The Goal is executed with signal processing disabled. This
avoids that e.g., thread_signal(Thread, abort)
kills the
exit handler rather than the thread in the case the body of Thread
has just finished when the signal arrives.