spawn ( quot name -- thread )
Factor handbook » The language » Co-operative threads » Starting and stopping threads

Next:spawn-server ( quot name -- thread )


Vocabulary
threads

Inputs
quota quotation
namea string


Outputs
threada thread


Word description
Spawns a new thread that will execute the quotation given; the name is for debugging purposes. The new thread is added to the end of the run queue.

The new thread begins with an empty data stack, an empty retain stack, and an empty catch stack. The name stack is inherited from the parent thread but may be cleared with init-namestack.

Notes
The recommended way to pass data to the new thread is to explicitly construct a quotation containing the data, for example using curry or compose.

Examples
A simple thread that adds two numbers:
1 2 [ + . ] 2curry "Addition thread" spawn drop yield

A thread that counts to 10:
USING: math.parser threads ; [ 10 <iota> [ number>string print yield ] each ] "test" spawn drop 10 [ yield ] times 0 1 2 3 4 5 6 7 8 9


Definition


: spawn ( quot name -- thread ) <thread> [ (spawn) ] keep ;