Starvation issue with Coro and AnyEvent

Laurent laurent.aml at gmail.com
Fri Jan 24 21:16:33 CET 2014


Marc,

On Thu, Jan 23, 2014 at 6:22 PM, Marc Lehmann <schmorp at schmorp.de> wrote:

> On Thu, Jan 23, 2014 at 03:35:30PM -0500, Laurent <laurent.aml at gmail.com>
> wrote:
> > The issue is that the "starved" coro never wakes up when the "greedy" one
> > runs.
>
> Right, you would need to give the event loop a higher priority (or your
> busy thread a lower one, or explicitly poll for events) - the rules are
> explained in the Coro::AnyEvent manpage.
>

Thanks a lot for your explanation.
Replacing 'cede' with 'Coro::AnyEvent::poll' is the solution I was looking
for.

BTW, the Coro::AnyEvent::poll doc has a little typo in the example: a colon
instead of a semicolon after 'poll'.


Fixed test program:

use common::sense;
use EV;
use AnyEvent;
use Coro;
use Coro::AnyEvent;

AnyEvent::detect;
my $loop = async { EV::loop; };

my $starving = async {
  while () {
    print STDERR '.';
    Coro::AnyEvent::sleep(0.1);
  }
};

my $cv = AE::cv;
my $greedy = async {
  print "\nStart eating...\n";
  for (my $i=10; $i>0; $i--) {
    my $t = AE::time + 0.1;
    while (AE::time < $t) {
      # Active wait.
    }
    print STDERR ($starving->is_ready ? '+' : '-');
    Coro::AnyEvent::poll;
  }
  print "\ndone\n";
  $cv->send;
};

$cv->recv;
exit 0;


Gives:

c:\test>perl test.pl
.
Start eating...
-+.+.+.+.+.+.+.+.+.
done


-- Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20140124/7f113860/attachment.html>


More information about the anyevent mailing list