Actually, this is Luca's code (the coroutine-based generator).

(I am the guilty party to blame for the naive idea to try to add threads to
it : I had the Goroutine/channel concept of Go in mind and was trying to
emulate it by attempting to combine Luca's generator with threads).

:)

Serge.



On Wed, Jul 13, 2011 at 3:02 PM, Jim Peters <j...@uazu.net> wrote:

> Serge Hulne wrote:
> > Here is a further development of the idea of Luca Bruno about a Vala
> > implementation for Generators:
> >
> > Simulating  Go's goroutines and channels in Vala:
> >
> > Basically the idea is to start as many threads as needed (which play the
> > role of Go' goroutines) and to recuperate their output from a "Generator"
> > (which plays the role of the "Go" channel form which the result from a
> given
> > thread can be pulled):
>
> To clarify after all the confusion (partly my fault -- sorry):
>
> - Luco's Generator doesn't need threads, and it doesn't need a main
>  loop either, even though it is using 'async'
>
> - The whole thing runs in a single thread
>
> A few thoughts:
>
> - 'async' is really powerful in Vala, but also quite hard to
>  understand.  (I wish I could find a high-level design document
>  explaining async in GIO.)
>
> - Its implementation of coroutines doesn't require another stack, it
>  holds its state in a private data structure.  When it does a 'yield'
>  it returns to the caller, and when it is next given control, it
>  resumes the previous point of execution using a switch and 'goto'.
>
> - Probably it could all go a lot faster if less housekeeping was going
>  on, but I haven't seen Go's implementation either, so who knows how
>  it compares.  Anyway, Vala always puts functionality first and
>  optimisation second, which suits people who want to get the job done
>  now instead of waiting for the ultimate language to arrive, so that
>  seems okay.
>
> Attached is a version of Serge's code with all the thread stuff cut
> out, which still works.
>
> Jim
>
> /////////////////////////////////
> using Posix;
>
> abstract class Generator<G> {
>    private bool consumed;
>    private unowned G value;
>    private SourceFunc callback;
>
>    public Generator () {
>        helper ();
>    }
>
>    private async void helper () {
>        yield generate ();
>        consumed = true;
>    }
>
>    protected abstract async void generate ();
>
>    protected async void feed (G value) {
>        this.value = value;
>        this.callback = feed.callback;
>        yield;
>    }
>
>    public bool next () {
>        return !consumed;
>    }
>
>    public G get () {
>        var result = value;
>        callback ();
>        return result;
>    }
>
>    public Generator<G> iterator () {
>        return this;
>    }
> }
>
> class IntGenerator : Generator<int> {
>    protected override async void generate () {
>        for (int i=0; i < 10; i++) {
>             if (i%2 ==0) yield feed (i);
>        }
>    }
> }
>
> class IntGenerator_1 : Generator<int> {
>    protected override async void generate () {
>        for (int i=0; i < 10; i++) {
>             if (i%2 !=0) yield feed (i);
>        }
>    }
> }
>
> int main(string[] args) {
>
>    var gen = new IntGenerator();
>    var gen_1 = new IntGenerator_1();
>
>    print("\n\nResults computed in first generator\n");
>
>    var i=0;
>    foreach (var item in gen) {
>        if (i<10) Posix.stdout.printf("%i\n", item);
>        i++;
>    }
>
>     print("\n\nResults computed in the second generator\n\n");
>
>    i=0;
>    foreach (var item in gen_1) {
>        if (i<10) Posix.stdout.printf("%i\n", item);
>        i++;
>    }
>
>    return 0;
> }
>
> /////////////////////////////////
>
> --
>  Jim Peters                  (_)/=\~/_(_)                 j...@uazu.net
>                          (_)  /=\  ~/_  (_)
>  UazĂș                  (_)    /=\    ~/_    (_)                http://
>  in Peru            (_) ____ /=\ ____ ~/_ ____ (_)            uazu.net
>
_______________________________________________
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to