1.3.1. test/regress/nd-1.01.01

Start felix section to test/regress/nd-1.01.01-0.flx[1 /1 ]
     1: #line 2536 "./lpsrc/flx_regress.pak"
     2: //Check pthreads
     3: //Check pchannels
     4: #import <flx.flxh>
     5: open System;
     6: 
     7: include "flx_faio";
     8: include "pthread";
     9: 
    10: open Faio; // async I/O module
    11: open Pthread; // pre-emptive threading
    12: 
    13: var clock = mk_alarm_clock();
    14: 
    15: var nthreads, njobs = 3,4;
    16: print$ f"%d threads, %d jobs\n"$ nthreads,njobs;
    17: 
    18: // read delay, and wait that long, exit on 0
    19: proc worker(i:int, chan:pchannel[int])(){
    20:  print$ f"Starting pthread %d\n" i;
    21:  var &delay:int <- read chan;
    22:  whilst delay != 0 do
    23:    print$ f"Thread %i, Delay %d\n"$ i,delay;
    24:    sleep$ clock,double delay;
    25:    &delay <- read chan;
    26:  done;
    27: }
    28: 
    29: // start the worker pthreads
    30: var chan = mk_pchannel[int]();
    31: proc spawn_workers(i:int)
    32: {
    33:   if i > nthreads return;
    34:   spawn_pthread (worker$ i,chan);
    35:   spawn_workers$ i+1;
    36: }
    37: spawn_workers 1;
    38: 
    39: // dispatch jobs
    40: whilst njobs > 0 do
    41:   var delay = Cstdlib::rand () % 10 + 1;
    42:   print$ f"Dispatch job %d\n" delay;
    43:   write$ chan, delay;
    44:   --njobs;
    45: done;
    46: 
    47: // terminate workers
    48: whilst nthreads > 0 do
    49:   write$ chan, 0;
    50:   --nthreads;
    51: done;
    52: 
End felix section to test/regress/nd-1.01.01-0.flx[1]