File: prodcons3

package info (click to toggle)
libcoro-perl 6.570-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: ansic: 2,560; perl: 2,122; makefile: 14
file content (38 lines) | stat: -rw-r--r-- 620 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/perl

# the classical producer/consumer example, using a channel
# one process produces items, sends a signal.
# another process waits for that signal and
# consumed the item.

use Coro;
use Coro::Channel;
use Coro::Signal;

my $work = new Coro::Channel 3;
my $finished = new Coro::Signal;

async {
   for my $i (0..9) {
      print "produced $i\n";
      $work->put($i);
   }
   print "work done\n";
   $finished->send;
};

async {
   while () {
      my $i = $work->get;
      print "consumed $i\n";
   }
};

$finished->wait;

print "producer finished\n";

cede while $work->size;

print "job finished\n";