next up previous contents index
Next: 7.7.1 Predicates for Thread Up: 7. Multi-Threaded Programming in Previous: 7.6 Configuring the Multi-threaded   Contents   Index


7.7 Predicates for Multi-Threading

The predicates described in this section do not address tabling or dynamic code. With only a few minor deviations the provisional working standard described in [31] is supported. As a result, these predicates are substantially the same as those in SWI, YAP, and other Prologs.

thread_create(+Goal,ThreadId,+OptionsList)
thread
When called from thread $T$, this predicate creates a new XSB thread $T_{new}$ to execute Goal. When goal either succeeds, throws an unhandled error, or fails, $T_{new}$ exits, but thread_create/2 will succeed immediately, binding ThreadId to the XSB thread id of $T_{new}$. Goal must be callable, but need not be fully instantiated. No bindings from Goal are passed back from $T$ to $T_{new}$, so communication between $T_{new}$ and $T$ must be through tables, asserted code, message queues or other side effects.

OptionList allows optional parameters in the configuration for the initial size of XSB stacks, for aliases, and to indicate whether $T_{new}$ is to be created as detached. Note that XSB threads allow automatic stack allocation, so that the size options may be most useful for (32-bit) applications with very large numbers of threads. In this case, setting initial stack sizes to be small may allow more threads to be created on a given hardware platform. Also note that only XSB stacks are affected, the stack size of the underlying Pthread remains unaltered.

Error Cases

thread_create(+Goal,-ThreadId)
thread
Acts as thread_create(Goal,ThreadId,[]).

thread_join(+Threads_or_aliases,-ExitDesignators)
thread
When thread_join/2 is called by thread $T$, Threads_or_aliases must be instantiated to either 1) an XSB thread id or alias; or 2) a list where each element is an XSB thread id or an alias; ExitDesignators must be uninstantiated. The action of the predicate is to suspend $T$ until all of the threads denoted by Threads_or_aliases have exited. At this time, any remaining resources for the threads in ThreadIds will have been reclaimed. Upon success ExitDesignators is either a the thread status of the associated thread (see page [*]) or a list of such elements.

Error Cases

thread_exit(+ExitTerm)
thread
Exits a thread $T$ with ExitTerm after releasing any mutexes held by $T$, freeing any thread-specific memory allocated for $T$ (we hope). ExitTerm will be used if the caller of $T$ joins to $T$, but will be ignored in other cases. There is no need to call this routine on normal termination of a thread as it is called implicitly on success or (final) failure of a thread's goal.

Error Cases

thread_self(?ThreadId_or_Alias)
thread
If ThreadId is an atom, unifies ThreadId_or_Alias with an alias of the calling thread. Otherwise, unifies ThreadId_or_Alias with the XSB thread id of the calling thread. There are no error conditions.

thread_detach(+Thread_or_Alias)
thread
Detaches a joinable thread denoted by Thread_or_Alias so that all resources will be reclaimed upon its exit. The thread denoted by ThreadId will no longer be joinable, once it is detached. If Thread_or_Alias has already exited, all resources used by Thread_or_Alias are removed from the system.

Error Cases

thread_cancel(+ThreadId)
thread
Cancels the XSB thread denoted by ThreadId. The cancellation does not use Pthread cancellation mechanisms, rather it uses XSB's interrupt mechanism to set ThreadId's interrupt vector. When this interrupt vector is checked, ThreadId will throw a thread cancellation error, which can be caught within ThreadId like any other error. Usually, this means that ThreadId will abort and exit. Note that the high-level upon which thread cancellation is implemented allows for simplicity and portability of thread cancellation 7.4.

The main XSB thread cannot be cancelled; apart from that any thread can cancel any other thread.

Error Cases

thread_yield
thread
Make the calling thread ready to be run after other threads of the same priority. This predicate relies on the real-time extensions to Pthreads specified in POSIX 1b, and may not be available on all platforms.

Error Cases

usleep(+Microseconds)
thread
Causes the calling thread to sleep approximately Microseconds before resuming.

Error Cases



Subsections
next up previous contents index
Next: 7.7.1 Predicates for Thread Up: 7. Multi-Threaded Programming in Previous: 7.6 Configuring the Multi-threaded   Contents   Index
Terrance Swift 2007-10-05