Got some interesting (to me) data on this comparison. After finding a way to track memory and cpu usage for child processes, here's how threads vs forks stacks up:

threads.pm: WC: 13.76s Usr: 0.39s Sys: 0.07s CSv/f: 0/0 IOops: 0/0 Sigs: 0 Swaps: 0 PF: 2500/1265 Msg: 0/0 Me +m: 65499 forks.pm: WC: 18.30s Usr: 0.33s Sys: 0.22s CSv/f: 0/0 IOops: 0/0 Sigs: 0 Swaps: 0 PF: 14427/2524 Msg: 0/0 M +em: 5454

These come from running the exact same tests ~20 times, varying only the use of threads.pm vs forks.pm. The code for this test is below, it does do a little bit of data sharing (since my intended usage model will as well).

Interesting to note that forks.pm does use far less memory, but gets far more page faults, and also uses more cpu time. Guess it depends which is more valuable to you. As stated by others above, forks.pm isn't necessarily the ideal implementation for using forks, but rather a convenient one because it uses the threads.pm interface.

Here's the code I used. For both threads and forks, I ran 9 times with 10 threads, 6 with 20, 3 with 50, and 3 with 100.

# which threading model do we want? use threads; use threads::shared; #use forks; use forks::shared; $num_threads = shift(@ARGV) || 10; $nonshared_var = 1; my $shared_var : shared = 1; foreach (0..$num_threads-1) { # create a new thread push(@threads,threads->new(\&thread_sub)); } foreach $thread (@threads) { # wait for threads to finish $thread->join(); } printf "Orig thread done\n"; sub thread_sub { my($count) = 20*rand() + 1; my($random); printf "Thread %d started\n",threads->tid(); # do some stuff while ($count-- > 0) { $random = (5*rand()) | (time() & 0x3); $nonshared_var += int($random); lock($shared_var); $shared_var += int($random); sleep($random); printf(" Thread %d loop %d: random=%d, nonshared_var=%d, ". "shared_var=%d\n",threads->tid(),$count,$random,$nonsha +red_var, $shared_var); threads->yield(); } printf("Thread %d DONE!\n",threads->tid()); }

In reply to Re: latest on ithreads vs forks? by Anonymous Monk
in thread latest on ithreads vs forks? by Anonymous Monk

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.