The ability to pass thread contexts into query and command functions allows a great deal of flexibility 3.4. Once XSB is initialized, XSB threads can be created from C and can execute independently of each other, effectively giving the ability for different calling threads to query XSB in a mechanism reminiscent of database cursors.
Figure 3.4 illustrates a very simple example of this. XSB is initialized and the file edb.P consulted exactly as in Figure 3.4. However, the function xsb_ccall_thread_create() causes the XSB thread p_th to create a new thread, causes the new thread to call the same command loop as the main thread, and sets r_th to point to the context of the new thread. The new thread r_th can be used for commands or queries just as p_th. Figure 3.4 shows that queries to the two threads can be interleaved, and errors for both threads can be checked and reported independently.
It is important to note that since each thread created by xsb_ccall_thread_create() goes into a command-loop similar to the command loop, it will stay around until it is explicitly killed or until XSB is closed. The call
xsb_kill_thread(r_th);is needed to make r_th to exit. Once a thread is exited, all of its data structures will be freed, including those that support xsb_get_error_type() and xsb_get_error_message() 3.5.