1 |
root |
1.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::Process; |
10 |
|
|
use Coro::Channel; |
11 |
|
|
use Coro::Signal; |
12 |
|
|
|
13 |
|
|
my $work = new Coro::Channel 3; |
14 |
|
|
my $finished = new Coro::Signal; |
15 |
|
|
|
16 |
|
|
async { |
17 |
|
|
for my $i (0..9) { |
18 |
|
|
print "produced $i\n"; |
19 |
|
|
$work->put($i); |
20 |
|
|
} |
21 |
|
|
print "work done\n"; |
22 |
|
|
$finished->send; |
23 |
|
|
}; |
24 |
|
|
|
25 |
|
|
async { |
26 |
|
|
while () { |
27 |
|
|
my $i = $work->get; |
28 |
|
|
print "consumed $i\n"; |
29 |
|
|
} |
30 |
|
|
}; |
31 |
|
|
|
32 |
|
|
$finished->wait; |
33 |
|
|
|
34 |
|
|
print "producer finished\n"; |
35 |
|
|
|
36 |
|
|
yield while $work->size; |
37 |
|
|
|
38 |
|
|
print "job finished\n"; |
39 |
|
|
|