Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!boulder!sunybcs!rutgers!ucla-cs!zen!ucbvax!hplabs!hp-pcd!orstcs!budd From: budd@orstcs.cs.ORST.EDU Newsgroups: comp.lang.smalltalk Subject: Back to the future Message-ID: <245100012@orstcs> Date: Fri, 21-Aug-87 17:04:00 EDT Article-I.D.: orstcs.245100012 Posted: Fri Aug 21 17:04:00 1987 Date-Received: Sun, 23-Aug-87 12:10:52 EDT Lines: 85 Nf-ID: #N:orstcs:245100012:000:3022 Nf-From: orstcs.cs.ORST.EDU!budd Aug 21 14:04:00 1987 The following is a short article that will appear in the next edition of the Little Smalltalk newsletter; however I thought it might be of more general intrest since it is not specific to Little Smalltalk. The code is given in Little Smalltalk form, however it can be easily translated into other dialects. The rest of the newletter should be ready about the middle of september. --tim budd ---------------------------------------------------- I was recently reading about a nifty new idea in BBN Butterfly Lisp, a dialect of Scheme. The butterfly is a multiprocessor machine, and the new feature is one primitive they are experimenting with to provide the user with easy to use multiprocessing primitives. I will simplify a bit here, but basically the idea is to provide a new statement called ``future''. When the user evaluates an expression such as (future expression) What they get back is a box. At some future date the box will contain the value of the expression. The computation of the expression then takes place in parallel with the con- tinuted execution of the program. If at some later time they try to read the contents of the box, if the result of the execution has not yet been completed the running process is suspended until the expression is evaluated. Once the box has been filled, however, it can be read and reread as often as desired. In BBN Lisp this primitive is implemented using con- tinuations, something which is absent from Smalltalk. Never the less, a similar idea can be easily produced. We will create a new class, called Future. Instances of Future will respond to two message; eval:, which takes as argument a block and starts the task of evaluating it, and value, which returns the value of the expression. Here is the class description for future: Class Future | result sem | [ eval: aBlock sem <- Semaphore new: 0. [ result <- aBlock value. sem signal ] fork | value sem wait. sem signal. ^ result ] Here is one test case I tried using future: Class Test [ test | sum i x | sum <- 0. 'about to create the future' print. x <- Future new eval: [ (1 to: 100) do: [:i | sum <- sum + 1]. 'finished future' print. sum ]. 'created future' print. x value print. 'done with future' print ] And here is a the output from this test case: %st -i future.st Little Smalltalk Test new test about to create the future created future finished future 100 done with future Test Notice how the program continued execution, printing a message before execution of the ``future'' expression had completed.