Module thread

A Prolog thread is simply a Java thread that executes a Prolog call-in. The call-in can be created by the predicate thread_new/2 and the goal will be copied. The thread can then be started by the predicate thread_start/1. A thread need not be explicitly destroyed, it will automatically be reclaimed by the Java GC when not anymore used.

Examples:
?- thread_new((between(0,10,X),write(X),write(' '),fail; 
     nl), I), thread_start(I).
I = 0r5ab10801
0 1 2 3 4 5 6 7 8 9 10

A new thread will share the knowledgebase and the display input/output of the creating thread. On the other hand a new thread will have its own thread local predicates. A thread can be aborted by the predicate thread_abort/2 and thread_down/[2,3]. A thread can be killed by the predicate thread_kill/1. The later predicate should only be used in emergency situation, since the receiving Prolog call-in will not be able to properly clean-up.

The predicates thread_join/1 and thread_combine/[1,2] allow waiting for the termination of a thread. The predicates will block, fail or timeout when the thread is alive. Every thread can be joined and joining does not retrieve an exit code and/or an exit Prolog term. The predicate current_thread_flag/3 allows inspecting thread properties.

The following thread predicates are provided:

thread_sleep(M):
The predicate suspends the current thread for M milliseconds.
thread_current(T):
The predicate succeeds for the current thread T.
thread_new(C, T):
The predicate succeeds for a new thread T on the copy of the goal C.
thread_start(T):
The predicate succeeds for starting the thread T.
thread_abort(T, M):
The predicate succeeds for signalling the error message M to the thread T.
thread_down(T, M):
The predicate succeeds for signalling the error message M to the thread T. Otherwise the predicate fails.
thread_down(T, M, W):
The predicate succeeds for signalling the error message M to the thread T in the timeout W. Otherwise the predicate fails.
thread_kill(T):
The predicate succeeds for killing the thread T.
thread_join(T):
The predicate succeeds when the thread T has terminated.
thread_combine(T):
The predicate succeeds when the thread T has terminated. Otherwise the predicate fails.
thread_combine(T, W):
The predicate succeeds when the thread T has terminated in the timeout W. Otherwise the predicate fails.
current_thread_flag(T, F, V):
The predicate succeeds for the value V of the flag F for the thread T.
set_thread_flag(T, F, V):
The predicate sets the flag F to the value V for the thread T.

The following thread thread flags are provided:

sys_thread_name:
The name of the thread. The flag cannot be changed.
sys_thread_status:
The status of the thread. The flag cannot be changed.
sys_thread_group:
The group of the thread. The flag cannot be changed.

The following thread Prolog flags are provided:

sys_cpu_count:
The flag returns the number of logical cores of the current process. The flag cannot be changed.
sys_runtime_version:
The flag returns the version of the Java language runtime of the current process. The flag cannot be changed.


Comments