Re: [Vala] [Erratum]Further speculations on couroutines, generators and threads : Emulating Go's goroutines and channels in Vala



Erratum, there was a omission in my previous post, here is the complete
snippet:

On Tue, Jul 12, 2011 at 9:25 AM, Serge Hulne <serge hulne gmail com> wrote:



If the code proposed by Luca was stored separately in its own file (or
library), then all one would have to to to use use this
goroutine/channnels/-ike construct would go like:

/////////////////////
using Posix;



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);
        }
    }
}


IntGenerator    gen;
IntGenerator_1  gen_1;

void* thread_func() {
    //Posix.stdout.printf("Thread running.\n");
    gen = new IntGenerator();
    return null;
}

void* thread_func_1() {
    //Posix.stdout.printf("Thread running.\n");
    gen_1 = new IntGenerator_1();
    return null;
}


int main(string[] args) {

    if (!Thread.supported()) {
        Posix.stderr.printf("Cannot run without threads.\n");
        return 1;
    }

    try {
        unowned Thread<void*> thread = Thread.create<void*>(thread_func,
true);
        thread.join();

        unowned Thread<void*> thread_1 =
Thread.create<void*>(thread_func_1, true);
        thread_1.join();

    } catch (ThreadError e) {
        return 1;
    }

    print("\n\nResults computed in first thread\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 thread\n\n");

    i=0;
    foreach (var item in gen_1) {
        if (i<10) Posix.stdout.printf("%i\n", item);
        i++;
    }


    return 0;
}
/////////////////////








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]