File: prodcons2

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 (36 lines) | stat: -rw-r--r-- 665 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
#!/usr/bin/perl

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

use Coro;
use Coro::Semaphore;

my $produced = new Coro::Semaphore 0;
my $finished = new Coro::Semaphore 0;

async {
   for my $i (0..9) {
      print "produced $i\n";
      push @buffer, $i;
      $produced->up;
      cede if @buffer > 5; # simulate memory pressure ;)
   }
   print "work done\n";
   $finished->up;
};

async {
   while () {
      $produced->down;
      my $i = shift @buffer;
      print "consumed $i\n";
   }
};

$finished->down;

print "job finished\n";