Thread creation

A thread is a unit for assigning computation, usually evaluation of a lisp form. Threads in EusLisp are represented by instances of the thread class. This object is actually a control port of a thread to pass arguments and result, and let it start evaluation, rather than the thread's entity representing the context.



sys:make-thread num &optional (lsize 32*1024) (csize lsize) [function]

creates num threads with lsize words of Lisp stack and csize words of C stack, and put them in the system's thread pool. All the threads in the thread pool is bound to sys:*threads*, which is extended each time make-thread is called. By the thread function, a computation is assigned to one of free threads in the thread pool. Therefore it is not a good idea to change stack sizes from thread to thread, since you cannot control which thread is assigned to a specific computation.


sys:*threads* [variable]

returns the list of all the threads created by make-threads.


sys::free-threads [function]

returns the list of threads in the free thread pool. If the result is NIL, new commitment of a task to a thread is blocked until any currently running threads finish evaluation or new threads are created by make-thread in the free thread pool.


sys:thread func &rest args [function]

picks up one free thread from the thread pool, and assigns it for evaluation of (func . args). Sys:thread can be regarded as asynchronous funcall, since sys:thread applies func to the spread list of args but it does not accept the result of the function application. Rather, sys:thread returns the thread object assigned to the funcall, so that the real result can be obtained later by sys:wait-thread.


(defun compute-pi (digits) ...)
(setq trd (sys:thread \#'compute-pi 1000)) ;assign compute-pi to a thread
...  ;; other computation 
(sys:wait-thread trd) ;get the result of (compute-pi 1000)

sys:thread-no-wait func &rest args [function]

assigns computation to one of free threads. The thread is reclaimed in the free thread pool when it finishes evaluation without being wait-thread'ed.


sys:wait-thread thread [function]

waits for thread to finish evaluation of funcall given by the sys:thread function, and retrieves the result and returns it. Sys:wait-thread is mandatory if the thread is assigned evaluation by sys:thread because the thread is not returned to the free thread pool until it finishes transferring the result.


sys:plist &rest forms [macro]

evaluates forms by different threads in parallel and waits for the completion of all evaluation, and the list of results is returned. Sys:plist may be regarded as parallel-list except that each form listed must be a function call.


k-okada 2013-05-21