6. my @urllist = read_list();
my @pids;
for my $url (@urllist) {
my $pid = fork();
if ($pid == 0) {
print get($url);
exit;
} elsif ($pid > 0) {
push @pids, $pid; # ID
}
}
waitpid($_, ...) for @pids; #
# do other nonconcurrent jobs
7. my @pids;
for ( 1 .. 20) {
my $pid = fork;
if ($pid == 0) {
while (1) {
my $task = lock_and_get_task();
do_task($task);
sleep 1;
}
exit;
} elsif ($pid > 0) {
push @pids, $pid;
}
}
waitpid($_, ...) for @pids;
9. use threads;
sub load_url {
print get($_[0]);
}
my @urllist = read_url_list();
my @tids;
for my $url (@urllist) {
my $tid = threads->create(&load_url, $url);
push @tids, $tid; # ID
}
$_->join for @tids; # waitpid
# do other things
10. use threads;
use threads::shared;
my @queue : shared;
for (1 .. 6) {
threads->create( sub {
while (1) {
my $task;
{ lock @queue;
$task = shift @queue;
}
do_task($task);
threads::sleep(1);
}
}
}
while (1) { generate_task(@queue); threads::sleep(1); }
14. Coro
use Coro;
async {
# some asynchronous thread of execution
print "2n";
cede; # yield back to main
print "4n";
};
print "1n";
cede; # yield to coro
print "3n";
cede; # and again