Re: [Vala] Question about a basic coroutine example



On Thu, 2011-07-07 at 22:23 +0200, Luca Dionisi wrote:
2. The sleep function from posix, tells the O.S. to put to sleep the
entire process, not just one function.
If you want a function similar to "sleep" that acts in a cooperative
manner you should create a function that goes something like this:

async void coop_sleep(int s)
{
  int ms = s * 1000;
  for (int i = 0; i < ms; i++)
  {
    Idle.add(coop_sleep.callback);
    yield;
    Posix.usleep(1000);
  }
}

That is:
 * put the entire process to sleep for small periods (1000
microseconds = 1 millisecond)
    you need this, otherwise you'll have a cpu hogging application
 * pass the schedule (yield)
 * repeat for the needed times, or until a certain time arrives.

It would be better to use something like this:

        Timeout.add (2000, foo1.callback);
        yield;

Regards,
Jürg




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