July 7th Tuesday (七月 七日 火曜日)

来源:互联网 发布:手机常用月历软件 编辑:程序博客网 时间:2024/05/17 06:14

Threads

  Scheme supports multiple threads of evaluation. Threads run concurrently, in the sense that one thread can preempt
another without its cooperation, but threads currently all run on the same processor (i.e., the same underlying OS process
and thread).

  Threads are created explicitly by functions such as thread. In terms of the evaluation model, each step in evaluation
actually consists of multiple concurrent expressions, up to one per thread, rather than a single expression. The expressions
all share the same objects and top-level variables, so that they can communicate through shared state. Most evaluation steps
involve a single step in a single expression, but certain synchronization primitives require multiple threads to progress
together in one step.

  In addition to the state that is shared among all threads, each thread has its own private state that is accessed through
thread cells. A thread cell is similar to a normal mutable object, but a change to the value inside a thread cell is seen only
when extracting a value from the cell from the same thread. A thread cell can be preserved; when a new thread is created,
the creating thread's value for a preserved thread cell serves as the initial value for the cell in the created thread.
For a non-preserved thread cell, a new thread sees the same initial value (specified when the thread cell is created) as
all other threads.