1. You told the thread to stop only after it stopped, which can never be satisfied.
    use threads; $| ++; threads->create(\&a); while (1) {} sub a { eval(threads->self->join);#Waiting for Godot print "before return\n";#will not show up }
    Here is one normal way to use join:
    use threads; $| ++; $child = threads->create(\&a); $child->join; # I am wating the $child thread to finish first print "Main thread stop\n"; sub a { for (1..5) { print "child is still counting, $_\n"; sleep(3); } print "child thread stop\n"; }
  2. Join provides a kind of coarse method to synchronize, just like what waitpid provided for processes. One process or thread stalled until the others caught up and finished. For finer methods of synchronization, please consider lock, condition, and semaphore. Let's use fork and waitpid to do something similar as what we did in point 1:

    This works:
    $| ++; if (($chld = fork()) == 0) { sleep(3); print "child exit\n"; } else { waitpid $chld, 0; print "parent exit\n"; }
    This hangs:
    $| ++; waitpid $$, 0; #wait for Godot print "exit\n";
  3. In real life, you always need to put an up limit on the number of threads you can have. Where is the limitation? it really depends on your context, your every bit of effort tuning your multi-threaded applications will be appreciated.
In a multi-threading program, always turn on $| for debuging, so you are not confused.

In reply to Re: Fun with threads by pg
in thread Fun with threads by znu

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.