💾 Archived View for gems.geminet.nl › bits › mit-scheme-threads.gmi captured on 2023-11-14 at 07:49:10. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
originally from:
http://web.mit.edu/benmv/6.001/www/threads.txt
MITScheme provides an implementation of preemptive multithreading.
However, it's completely undocumentated. Here's what I have
discovered.
(create-thread root-continuation thunk)
Dunno what root-continuation is really used for. I don't quite get
%within-continuation. Passing #f means it uses some happy default.
The thunk is executed in a new thread. Return value of thunk is
the exit-value of the thread.
(thread-timer-interval)
Default is 100. Measure of time between preemptions.
(set-thread-timer-interval! interval)
Setter for the same.
(sleep-current-thread interval)
Current thread next executes after <interval> milliseconds.
(yield-current-thread)
Let some other thread run now.
(stop-current-thread)
Current thread is not run again until restarted explicitly.
(restart-thread thread discard? event)
Stopped thread is restarted. events are thrown away if
discard? is true. If event is not #f, it's delivered to the
thread.
(exit-current-thread val)
Halt current thread. val is stored as the exit-value of the thread.
Better to just let the thunk complete. You can use a continuation
for this purpose:
(create-thread #f
(lambda ()
(call-with-current-continuation
(lambda (exit)
;; do stuff
;; to exit thread, (exit <val>)
;; where val is the desired exit value
))))
(thread/exit-value thread)
Get exit value of thread. It's not exported, so:
(define (thread/exit-value thread)
((in-package (->environment '(runtime thread))
(lambda (thread)
(thread/exit-value thread)))
thread))
(threads-list)
List of all threads. Two threads exist to start,
the eval thread, and the edwin thread. Dead threads
will remain in the list. To see status:
(map thread-execution-state (threads-list))
(make-thread-mutex)
Creates a mutex. If can be used to synchronize between
threads. For a critical section of code, use:
(with-thread-mutex-locked mutex thunk)