use strict; use warnings; use threads (stack_size => 64*4096); use IO::Handle (); # extra stability my @car_links = (1..200); my ($username, $password, $fh, @known_cars); sub reserve_car { } sub write_url_to_known_cars { } my $count = 0; for my $c (@car_links) { CREATE: my $thr = threads->create(sub { # Reserve for 10 seconds total. for (1..1) { reserve_car($c, $username, $password); sleep 10; } threads->exit(0); }); if (!defined $thr) { while () { $count++; warn "cannot spawn thread, waiting -- $count\n"; my $thr_exited = 0; for my $thr (threads->list(threads::joinable)) { $thr->join; $thr_exited = 1; } $thr_exited ? last : sleep(1); } goto CREATE; } push @known_cars, $c; write_url_to_known_cars($c, $fh); } print "reaping threads\n"; $_->join for threads->list;