Threads

The following are exported by the THREADS structure.

(SPAWN thunk)
(SPAWN thunk name)
  Create and schedule a new thread that will execute <thunk>.  The optional
  name is used when printing the thread.

(RELINQUISH-TIMESLICE)
  Let other threads run for a while.

(SLEEP time)
  Sleep for <time> milliseconds.

(TERMINATE-CURRENT-THREAD)
  Kill the current thread.

(THREAD? thing)
  #T if thing is a thread, #F otherwise.

(THREAD-NAME thread)
(THREAD-UID thread)
  For printing debugging information.

-----

The following are exported by the LOCKS structure.

(MAKE-LOCK) => lock
(OBTAIN-LOCK lock)
(RELEASE-LOCK lock)
  Locks are semaphores.

-----

The following are exported by the PLACEHOLDERS structure.

(MAKE-PLACEHOLDER) => placeholder
(PLACEHOLDER-VALUE placeholder) => value of placeholder
(PLACEHOLDER-SET! placeholder value)
(PLACEHOLDER? thing) => #t or #f
  Attempts to reference a placeholder before it has been set cause the
  referencing thread to block.  Setting a placeholder to two different
  values is an error.  (Previous versions of Scheme 48 called these
  `condition variables', which turn out to be somewhat different.)

-----

Threads and the command interpreter.

Each level of the command interpreter has its own set of active
threads.  Moving to a new level, for example when an error occurs,
halts all threads belonging to the previous level.  Resuming the
a level causes its associated threads to continue running.

The ,threads command inspects the threads running in the stopped
command level.


    > ,open threads
    > (define (foo) (sleep 1000) (display "Hi") (newline) (foo))
    > (spawn foo 'my-thread)
    > Hi
    (begin (sleep 10000) (display "Done") (newline))
    Hi
    
    Interrupt: keyboard
    1> (sleep 5000)
    ; note that the Hi thread doesn't run in this command level
    1> ,proceed 0
    Hi
    ; but it resumes when we resume this level
    Hi
    Done
    > Hi
    Hi
    Hi
    
    Interrupt: keyboard
    1> ,threads
    '(#{Thread 28 my-thread} #{Thread 27 command-loop})
    
     [0] '#{Thread 28 my-thread}
     [1] '#{Thread 27 command-loop}
    inspect: