Hi fluks,

The following are modified threads and fork demonstrations. They work on Linux including 32-bit and 64-bit Windows.

For threads, one may need to increase stack_size depending on the application. Not reaping threads will likely cause recent threads to not complete and exit prematurely. Ditto for fork on the Windows platform. Tip: Loading IO::Handle before spawning workers is beneficial. This saves workers from having to load this particular module and dependencies individually.

threads

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;

fork

use strict; use warnings; use POSIX ":sys_wait_h"; use IO::Handle (); # extra stability my @car_links = (1..100); my ($username, $password, $fh, @known_cars); sub reserve_car { } sub write_url_to_known_cars { } my $count = 0; my %pids; for my $c (@car_links) { FORK: my $pid = fork(); if (!defined $pid) { while () { $count++; warn "cannot spawn child, waiting -- $count\n"; my $child_exited = 0; for my $pid (keys %pids) { # support negative PID value on Windows my $ret = waitpid($pid, WNOHANG); if ($ret < -1 || $ret > 0) { delete $pids{$pid}; $child_exited = 1; } } $child_exited ? last : sleep(1); } goto FORK; } elsif ($pid == 0) { # Reserve for 10 seconds total. for (1..1) { reserve_car($c, $username, $password); sleep 10; } exit; } else { $pids{$pid} = undef; } push @known_cars, $c; write_url_to_known_cars($c, $fh); } print "reaping children\n"; waitpid($_, 0) for (keys %pids);

Regards, Mario


In reply to Re^2: Fork and creating a thread exits the process by marioroy
in thread Fork and creating a thread exits the process by fluks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.