Depending on your platform, fork() may be implemented via threads, for example on Windows. Here, using fork doesn't improve things.

Have you checked that your process isn't bound by the time to read each file? If the time it takes to read each file from disk or from the network dominates your processing, then parallelizing will only improve the overall processing time if you are not yet at the upper IO limit.

You could somewhat easily test this by manually launching your programs for three files (or how many CPUs you have idle). If that improves the processing time, then you have something to gain from a parallel approach.

If you've determined that parallelizing gains you something, I would use the "worker pool" approach that you can find in most posts here by BrowserUk about threads:

my @files = @ARGV; my $NUM_CPUS = 4; # or whatever my $jobs = Thread::Queue->new(@files); # We use undef as "end of jobs" marker $jobs->enqueue( undef ) for $NUM_CPUS; my @threads = map { threads->create(\&process); } 1..$NUM_CPUS; sub process { while( my $file = $jobs->dequeue ) { validate_the_file($file,$readOnlySchema); }; }; $_->join for @threads;

You may or may not see an improvement by moving $readOnlySchema into &process. Data "shared" across threads can be problematic if you're using XS modules like XML::LibXML.


In reply to Re: Perl modules that I can use for Multithreading by Corion
in thread Perl modules that I can use for Multithreading by elpis

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.