A new approach to multi-threading

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

For the group's consideration:

OPTIONAL FORK An alternative to tasks.

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

:NONAME ( -- ) _PARAM_0 CATCH ExitThread ; 1 CB: FORKED

: FORK ( xt -- handle ) 0 >R
>R 0 65536 FORKED R> 0 RP@ CreateThread R> DROP ;

\\

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!

For example,

VARIABLE FOO

: BAR 1000 0 DO 1 FOO +! 250 Sleep DROP LOOP ;

' BAR FORK .

.
Received on Wed Jan 19 2000 - 15:14:54 PST


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

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