Some problems can be conveniently solved using a consumer/producer model. A producer generates data, which a consumer must handle.
Special care must be taken to handle the situation where the producer generates data faster than the consumer can handle. In this case, the values must be queued until the consumer is ready.
The thread.ss collection provides a solution in the form of the consumer-thread function. The consumer-thread function receives a function f capable of handling one piece of data, and returns two values: a thread in which the consumer runs, and a function which the producer (the current thread) can use to send data to the consumer. The function will queue the data and return void immediately.
In the example below, the consumer uses two seconds to print each number, whereas the consumer generates the numbers instantaneously. The numbers are printed with delay of two seconds between.
The consumer-thread has an optional parameter init, a thunk which if present is called as the first thing in the new (consumer) thread.
-- JensAxelSoegaard - 02 May 2004