1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | # the classical producer/consumer example, using semaphores |
3 | # the classical producer/consumer example, using semaphores |
4 | # one process produces items, send s a signal. |
4 | # one process produces items, sends a signal. |
5 | # another process waits for that signal and |
5 | # another process waits for that signal and |
6 | # consumed the item. |
6 | # consumed the item. |
7 | |
7 | |
8 | use Coro; |
8 | use Coro; |
9 | use Coro::Semaphore; |
9 | use Coro::Semaphore; |
… | |
… | |
14 | async { |
14 | async { |
15 | for my $i (0..9) { |
15 | for my $i (0..9) { |
16 | print "produced $i\n"; |
16 | print "produced $i\n"; |
17 | push @buffer, $i; |
17 | push @buffer, $i; |
18 | $produced->up; |
18 | $produced->up; |
19 | yield if @buffer > 5; # simulate memory pressure ;) |
19 | cede if @buffer > 5; # simulate memory pressure ;) |
20 | } |
20 | } |
21 | print "work done\n"; |
21 | print "work done\n"; |
22 | $finished->up; |
22 | $finished->up; |
23 | }; |
23 | }; |
24 | |
24 | |