Re: A new approach to multi-threading

From: Rick VanNorman <rvn_at_forth.com>
Date: Wed, 19 Jan 2000 22:54:41 -0800

Me again. Slight revisions to the code. Now, 10 lines instead of 4.
Ouch. However, it now can convey an arbitrary amount of information
across the CreateProcess interface; in particular it is configured
to pass the current object and class pointers. This is necessary
to be able to use the FORKS> mechanism in a class.

Enjoy!
Rick

OPTIONAL FORK An alternative to tasks.

{ ====================================================================
(C) Copyright 1999 FORTH, Inc. www.forth.com Rick VanNorman
FORK, an alternative to tasks.
==================================================================== }

{ --------------------------------------------------------------------
param0 has a pointer to the startup data, which needs to be
released as soon as it is used. The callback will initialize
a dictionary, a user area, and the stacks. The data passed
is simply a pointer to a structure which contains
   +0 'this
   +4 'self
   +8 xt to run

local variables and objects may not be passed across this interface
-------------------------------------------------------------------- }

:NONAME ( -- )
   _PARAM_0 @+ 'THIS ! @+ 'SELF ! @ _PARAM_0 FREE DROP
   CATCH ExitThread ; 1 CB: FORKED

: FORK ( xt -- handle ) 0 >R
   4 CELLS ALLOCATE IF DROP INVALID_HANDLE_VALUE EXIT THEN
   DUP >R THIS !+ SELF !+ !
   0 65536 FORKED R> 0 RP@ CreateThread R> DROP ;

: FORKS> ( -- ) R> CODE> FORK ;

\\

{ --------------------------------------------------------------------
In SwiftForth, the callback mechanism builds a "virtual" forth machine
for code to execute on, complete with a user context and stacks. This
is exactly what a background thread needs to be and to do. So, I have
implemented FORK to do very simple multi-threading. Give FORK an
xt to execute; it will run as a separate thread either forever, or until
it completes, or until it throws. This task is allowed to return; it
is not required to be an infinite loop.

FORK returns the handle of the thread created. If the user desires to
manipulate the thread, he better keep up with the handle returned!

FORKS> lets you fork the remainder of a word; the side effect is that
it will return the handle as it exits. Sorta like DOES>
-------------------------------------------------------------------- }

{ --------------------------------------------------------------------
Simple example
-------------------------------------------------------------------- }

VARIABLE FOO
VARIABLE ZOT FOO ZOT !

: BAR OPERATOR'S SELF H. THIS H.
   1000 0 DO 1 ZOT @ +! 250 Sleep DROP LOOP ;

' BAR FORK

{ --------------------------------------------------------------------
Class/object based example
Please note that the word which does the activate is protected from
accidental use by the public word GO. This is essential if the code
is to prevent the accidental spawning of many, many threads.
-------------------------------------------------------------------- }

CLASS POO

   PROTECTED

   VARIABLE HTHREAD
   VARIABLE COUNTING

   : TEST ( -- hthread ) 0 COUNTING ! FORKS>
      BEGIN 1 COUNTING +! 100 :: Sleep DROP AGAIN ;

   PUBLIC

   : GO ( -- ) HTHREAD @ ?EXIT TEST HTHREAD ! ;

   : DOT ( -- ) COUNTING @ . ;

END-CLASS

POO BUILDS X1
POO BUILDS X2

.
Received on Wed Jan 19 2000 - 22:54:41 PST


Subscribe to our e-mail list service. It's free for all SwiftForth and SwiftX users!

This archive was generated 09-Feb-2012. Archive updated nightly.