The thread package provides support for multithreaded programming. This package is based on POSIX thread library.
Before using functions and variables in Thread Package, following function call should be made to dynamically load necessary library.
thread:createcreates and starts a new thread that executes fun. It returns a thread object that represents the new thread.
It recognizes following keyword arguments.
should be 'joinable or 'detached. Chooses whether the thread is created
in the joinable state or in the detached state. 'joinable is for
should be 'other, 'rr, or 'fifo. Selects the scheduling policy for the
thread. 'other is for |
|schedparam||should be an integer. Changes the scheduling priority for the thread. Default is 0.|
should be 'explicit or 'inherit. Chooses whether the scheduling policy
and scheduling parameter for the newly created thread are determined by
the values of |
should be 'system or 'process. Chooses the scheduling contention scope
for the created thread. 'system is for |
(thread:create (lambda () (display "Thread 1") (newline)) :detachstate 'detached) ==> #<thread>
#tif obj is a thread object, otherwise returns
thread:exitis called; or cancelled by
threadterminates by cencellation,
thread:joinreturns an unspecified value. Otherwise, it returns the value that the thread function returns or
PTHREAD_CANCEL_ENABLE, meaning that cancellation is to be enabled. 'disable is for
PTHREAD_CANCEL_DISABLE, meaning that cancellation is to be disabled. Returns the current cancellation state ('enable or 'disable).
(thread:setcancelstate 'disable) ==> enable
PTHREAD_CANCEL_DEFERRED, to keep the cancellation request pending until the next cancellation point. 'asynchronous is for
PTHREAD_CANCEL_ASYNCHRONOUS, to cancel the thread as soon as the cancellation request is received. Returns the current cancellation type ('deferred or 'asynchronous).
thread:with-cleanupreturns that value. In this case, cleanup-handler does nothing. If fun does not return (that is, terminated either by
thread:exitor cancellation), cleanup-handler is called with no argument on thread termination and
(define mutex (thread:make-mutex)) (begin (thread:mutex-lock mutex) (thread:with-cleanup (lambda () (thread:mutex-unlock mutex)) (lambda () (some-fun))) (thread:mutex-unlock mutex))
thread:mutex-lockreturns immediately. If the mutex is already locked by another thread,
thread:mutex-locksuspends the calling thread until the mutex is unlocked. Returns an unspecified value.
thread:mutex-trylocktries to lock the mutex. If the mutex is currently unlocked, the mutex is locked by the calling thread and
#t. If the mutex is currently locked, it does not block and returns
thread:cond-waitatomically unlocks the mutex and waits for the condition variable cond. The mutex must be locked by the calling thread on entrance to
thread:cond-wait. Before returning to the calling thread,
thread:cond-waitre-acquires mutex. Returns an unspecified value.
Atomically unlocks mutex and waits on cond, as
thread:cond-wait does, but it also bounds the duration of the
wait. If cond has not been signaled before the time specified by
sec and optional nanosec, mutex is re-acquired and
#f. Otherwise (that is,
cond is signaled) it returns
The time specified by sec and optional nanosec is an absolute time. If sec and nanosec (if supplied) are both 0, it corresponds to 00:00:00 GMT, January 1, 1970.
#fimmediately. Otherwise the behavior is the same as thread:wait-sem.
@SIG_SETMASK, the signal mask is set to newmask. If how is
@SIG_BLOCK, the signals specified by newmask are added to the current signal mask. If how is
@SIG_UNBLOCK, the signals specified by newmask are removed from the current signal mask.
newmask should be a signal set created and manipulated by signal-related functions See section Signal Handling.
(define sigset (make-sigset)) (sigemptyset sigset) (sigaddset sigset @SIGINT) (thread:sigmask @SIG_BLOCK sigset)
(define th (thread:create (lambda () (let loop () (loop))))) (thread:kill th @SIGINT)
thread:sigwait to work reliably, the signals being waited for
must be blocked in all threads, not only in the calling thread. The best
way to achieve this is block those signals before any threads are
(thread:create (lambda () (let ((sigset (make-sigset))) (sigemptyset sigset) (sigaddset sigset @SIGINT) (let loop () (let ((signo (thread:sigwait sigset))) (display "SIGINT received") (newline) (loop))))))
#tif thread1 and thread2 represent the same thread, otherwise returns
thread:join. Returns an unspecified value.
forkreturns. child handler will be called from the child process, just before
thread:atforkreturns an unspecified value.
thread:atfork can be called several times to install several sets
of handlers. At
fork time, prepare handlers are called in
LIFO order (last added, first called), while parent and
child handler are called in FIFO order (first added, first
thread:atfork uses following global variables:
@@atfork_prepare, @@atfork_parent, and @@atfork_child.
caris an integer representing the policy (@SCHED_OTHER, @SCHED_RR, or @SCHED_FIFO) and
cdris an integer representing the priority.
(thread:getschedparam (thread:self)) ==> (0 . 0)
Go to the first, previous, next, last section, table of contents.