Revision: | 1.5 |
Committed: | Mon Jul 23 02:14:19 2001 UTC (22 years, 11 months ago) by root |
Branch: | MAIN |
CVS Tags: | rel-2_0, rel-2_1, rel-1_1, rel-1_0, rel-1_9, rel-1_2, rel-1_5, rel-1_4, rel-1_7, rel-1_6, rel-1_31 |
Changes since 1.4: | +1 -1 lines |
Log Message: | *** empty log message *** |
# | Content |
---|---|
1 | #!/usr/bin/perl |
2 | |
3 | # the classical producer/consumer example, using semaphores |
4 | # one process produces items, send s a signal. |
5 | # another process waits for that signal and |
6 | # consumed the item. |
7 | |
8 | use Coro; |
9 | use Coro::Semaphore; |
10 | |
11 | my $produced = new Coro::Semaphore 0; |
12 | my $finished = new Coro::Semaphore 0; |
13 | |
14 | async { |
15 | for my $i (0..9) { |
16 | print "produced $i\n"; |
17 | push @buffer, $i; |
18 | $produced->up; |
19 | cede if @buffer > 5; # simulate memory pressure ;) |
20 | } |
21 | print "work done\n"; |
22 | $finished->up; |
23 | }; |
24 | |
25 | async { |
26 | while () { |
27 | $produced->down; |
28 | my $i = shift @buffer; |
29 | print "consumed $i\n"; |
30 | } |
31 | }; |
32 | |
33 | $finished->down; |
34 | |
35 | print "job finished\n"; |
36 |