| Revision: | 1.3 |
| Committed: | Mon Jul 23 02:14:19 2001 UTC (25 years, 2 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.2: | +1 -1 lines |
| Log Message: | *** empty log message *** |
| # | User | Rev | Content |
|---|---|---|---|
| 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::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 | root | 1.3 | cede while $work->size; |
| 36 | root | 1.1 | |
| 37 | print "job finished\n"; | ||
| 38 |