3. Felix threads -- Cooperative Multitasking

Felix supports event driven programming with a threading interface. This is variously known as cooperative multi-tasking, ultra-lightweight threading, coroutines, continuations, resumptions, in Felix these are called fthreads.

The start function starts a new thread in a stopped state.

If the thread does not read input, it can be executed by the run procedure. If it reads input, the send procedure is used to send data to the thread. Each send must correspond to a read operation dynamically.

A C++ exception is thrown, aborting the program, if you try to run a dead (terminated) thread, or send data to a terminated thread.

CAVEAT: At present, there is no check that the type of the data sent agrees with the type which is read.

Start C++ section to tut/examples/tut210.flx[1 /1 ]
     1: include "std";
     2: 
     3: print "Start .. "; endl;
     4: 
     5: proc printing() {
     6:   var s:string;
     7:   print "Hello ";
     8:   &s <- read;
     9:   print s; endl;
    10: 
    11:   print "This is ";
    12:   &s <- read;
    13:   print s; endl;
    14: 
    15:   print "Saying ";
    16:   &s <- read;
    17:   print s; endl;
    18: }
    19: 
    20: var x = start the printing;
    21: send[string] (&x) "World";
    22: send[string] (&x) "Felix";
    23: send[string] (&x) "Bye for now";
    24: 
    25: print ".. OK"; endl;
    26: 
End C++ section to tut/examples/tut210.flx[1]


3.1. Message chain using fthreads