s c h e m a t i c s : c o o k b o o k

/ ThreadRecipes / Cookbook.ProducerConsumerThreads

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Consumer/Producer Threads

Problem

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.

Solution

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. 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Discussion

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

CookbookForm
TopicType: Recipe
ParentTopic: ThreadRecipes
TopicOrder: 030

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest