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

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).



On Wed, Jul 13, 2011 at 3:02 PM, Jim Peters <jim 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
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.


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;

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

    print("\n\nResults computed in the second generator\n\n");

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

   return 0;


 Jim Peters                  (_)/=\~/_(_)                 jim uazu net
                         (_)  /=\  ~/_  (_)
 UazĂș                  (_)    /=\    ~/_    (_)                http://
 in Peru            (_) ____ /=\ ____ ~/_ ____ (_)  

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