Hi monks,

I'm new to threads, but I've heard of much benifit about threads for long time. So I decide to give up fork and choose threads in my next programme.
But I'm stuck at very start:( Below snippet is from perlthrtut, very simple.
use threads; $Param3 = "foo"; $thr = threads->new(\&sub1, "Param 1", "Param 2", $Param3); $thr = threads->new(\&sub1, @ParamList); $thr = threads->new(\&sub1, qw(Param1 Param2 Param3)); sub sub1 { my @InboundParameters = @_; print "In the thread\n"; print "got parameters >", join("<>", @InboundParameters), "<\n" +; } __OUTPUT__ D:\>perl thr_test.pl In the thread got parameters >Param 1<>Param 2<>foo< In the thread got parameters >< A thread exited while 2 threads were running. D:\>perl thr_test.pl In the thread got parameters >Param 1<>Param 2<>foo< A thread exited while 3 threads were running. D:\>perl thr_test.pl In the thread got parameters >Param 1<>Param 2<>foo< A thread exited while 3 threads were running.
I found that output is different with the running times. According to warnings, it seems some threads already quit when another thread running. All threads must quit all together? That's odd! Then I add 'sleep' in the sub route to try to delay thread quit like below.
use strict; use warnings; use threads; no warnings 'threads'; my @ParamList = qw (1 2 3); my $Param3 = "foo"; my $thr = threads->new(\&sub1, "Param 1", "Param 2", $Param3); $thr = threads->new(\&sub1, @ParamList); $thr = threads->new(\&sub1, qw(Param1 Param2 Param3)); sub sub1 { my @InboundParameters = @_; sleep 10; print "In the thread\n"; print "got parameters >", join("<>", @InboundParameters), "<\n" +; } __OUTPUT__ D:\>perl thr_test.pl A thread exited while 4 threads were running. D:\>perl thr_test.pl A thread exited while 4 threads were running. D:\>perl thr_test.pl A thread exited while 4 threads were running. D:\>perl thr_test.pl A thread exited while 4 threads were running.
Warnings is still tossed although the status seems better than before.

could any suggestions enlighten me? Thanks in advance!


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

In reply to Threads question by xiaoyafeng

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.