1 |
root |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
# the classical producer/consumer example. |
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::Signal; |
11 |
|
|
|
12 |
|
|
my $produced = new Coro::Signal; |
13 |
|
|
my $consumed = new Coro::Signal; |
14 |
|
|
my $finished = new Coro::Signal; |
15 |
|
|
|
16 |
|
|
async { |
17 |
|
|
for (0..9) { |
18 |
|
|
print "produced something\n"; |
19 |
|
|
$produced->send; |
20 |
|
|
$consumed->wait; |
21 |
|
|
} |
22 |
|
|
print "work done\n"; |
23 |
|
|
$finished->send; |
24 |
|
|
$idle; |
25 |
|
|
}; |
26 |
|
|
|
27 |
|
|
async { |
28 |
|
|
while () { |
29 |
|
|
$produced->wait; |
30 |
|
|
print "consuming something\n"; |
31 |
|
|
$consumed->send; |
32 |
|
|
} |
33 |
|
|
}; |
34 |
|
|
|
35 |
|
|
$finished->wait; |
36 |
|
|
|
37 |
|
|
print "job finished\n"; |
38 |
|
|
|