Hi,

I got curious about fthreads and decided to try implementing the
Shootout's "cheap concurrency" test [1].  However I'm encountering
very odd problems.

The main point of the test is to create 500 threads, and pass an
integer to each thread in succession (thread A sends to thread B,
thread B to C, etc.), incrementing the integer by one in each thread.
The guts of the Felix code I came up with is this:

def var i, val o = mk_ioschannel_pair[int] ();

proc worker(i: ischannel[int], o: oschannel[int])() {
    forever {
        var &x: int <- read i;
        write(o, x + 1);
    };
}

var t: int;
forall t in 1 upto 500 do
    val i' = i;
    def i, val o' = mk_ioschannel_pair[int] ();
    spawn_fthread$ worker(i', o');
done;

(you can find the rest at [2]).  o is the "first" channel, to which
the integer 0 is initially sent.  i is the "last" channel, from which
the final incremented integer is read.  Each worker thread reads from
the i of the previous thread (hence i is a var; it is updated each
time a new channel is created) and writes to a new output channel
(whose associated input channel is then assigned to i).

Unfortunately this doesn't seem to work at all: it seems that the
threads are all created, but die at their first read.  However if I
create only one thread instead of 500, it works as expected (the
integer is incremented by 10 by the time it exits).

Curiously, if I replace the thread-creation loop with this (seemingly
equivalent) code:

var t: int;
forall t in 1 upto 500 do
    val i', o' = mk_ioschannel_pair[int] ();
    spawn_fthread$ worker(i, o');
    i = i';
done;

then it doesn't work even for only one thread.

Am I misunderstanding the way fthreads work?  I was caught up at first
by the fact that they don't buffer anything (my first stab at the main
loop sequentially wrote to the first channel and then tried reading
the result) so maybe I'm still doing something silly.

Thanks,
Chris

[1] 
http://shootout.alioth.debian.org/gp4sandbox/benchmark.php?test=message&lang=all#about
[2] http://users.wpi.edu/~squirrel/temp/cheap-concurrency.flx

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to