| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# the classical producer/consumer example, using a channel |
| 4 |
# one process produces items, sends a signal. |
| 5 |
# another process waits for that signal and |
| 6 |
# consumed the item. |
| 7 |
|
| 8 |
use Coro; |
| 9 |
use Coro::Channel; |
| 10 |
use Coro::Signal; |
| 11 |
|
| 12 |
my $work = new Coro::Channel 3; |
| 13 |
my $finished = new Coro::Signal; |
| 14 |
|
| 15 |
async { |
| 16 |
for my $i (0..9) { |
| 17 |
print "produced $i\n"; |
| 18 |
$work->put($i); |
| 19 |
} |
| 20 |
print "work done\n"; |
| 21 |
$finished->send; |
| 22 |
}; |
| 23 |
|
| 24 |
async { |
| 25 |
while () { |
| 26 |
my $i = $work->get; |
| 27 |
print "consumed $i\n"; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
$finished->wait; |
| 32 |
|
| 33 |
print "producer finished\n"; |
| 34 |
|
| 35 |
cede while $work->size; |
| 36 |
|
| 37 |
print "job finished\n"; |
| 38 |
|