here's how to do something like select(), i.e. read from one of several
channels, which every is ready first:

proc multiplex (i1:ischannel[int], i2:ischannel[int], o:oschannel[int]) {
  spawn_fthread { while true do var x = read i1; write (o,x); done };
  spawn_fthread { while true do var x = read i2; write (o,x); done };
}

var i1,o1 = mk_ioschannel_pair[int]();
var i2,o2 = mk_ioschannel_pair[int]();
var i3,o3 = mk_ioschannel_pair[int]();

spawn_fthread { multiplex (i1, i2, o3); };
spawn_fthread { write (o1, 1); };
spawn_fthread { write (o2, 2); };
println$ read i3;
println$ read i3;


This particular code only handles two channels but is easily
generalised to an array or list. [Just remember closures built
on loops don't do what you might think!!!!!!]

Clearly the channels have to have the same type.
This multiplexer doesn't tell which channel provided
the data, but that is easy to fix (just make the output
channel type a pair: an identifier and the data)

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Keep yourself connected to Go Parallel: 
TUNE You got it built. Now make it sing. Tune shows you how.
http://goparallel.sourceforge.net
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to