Hi,

Here you have an example:

#!/usr/bin/perl -w $|++; use strict; use threads; use threads::shared; # Flag variable we use to stop the 2nd thread my $complete : shared = 0; # Create the thread my $thread = threads->create(\&dothis); # Code of main thread goes here sleep 5; # Change the flag variable so the thread ends $complete = 1; # Join the threads together $thread->join(); # End of the program exit; sub dothis { until($complete) { print "#"; sleep 1; } print "\n"; }

Take a look at threads.

But you can use fork and sending a kill signal to the child instead, I think it would be faster and more efficient.

For more complicated solution think about Event, Event::Lib or POE.

Update: This code is a example of threads, but of course for a simple stats of a file download is not necessary... as stated before. I supossed that you wanted a simple example to see how threads work in Perl.

Regards,

|fire| at irc.freenode.net

In reply to Re: multithreading sample by fmerges
in thread multithreading sample by sashac88

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.