title: Using maximum threads on multicore cpu
body: <p>at first, my perlskills are limited so please keep that in mind. ;)</p>
  
  <p>I have written a perlscript which indexes a directory and does something for each file in it.</p>
  
  <p>Processing a file takes some time. Between 1 - 5 mins each at a cpu load nearly 100% (one core). My idea is, cause i have a quadcore cpu, to process more then one file at once, which leads me to perl threads.</p>
  
  <p>So here are my questions.</p>
  
  <p>1) Is my assumption right, that perl threads are allocated automatically to multiple cores?</p>
  
  <p>2) I found this code example which does what i need, i think but i cannot figure out, how to keep always only 8 threads active. The example starts a static count of threads and is done when they are processed. However, in my case i have, lets say, 50 files to process but only 8 threads should be active at the same time. </p>
  
  <p>So it should be like this: 
  Read the directory, start 8 threads for the 8 first files and keep 8 threads working until all files are processed.</p>
  
  <pre><code>#!/usr/local/roadm/bin/perl
  # This is compiled with threading support
  
  use strict;
  use warnings;
  use threads;
  use threads::shared;
  
  print "Starting main program\n";
  
  my @threads;
  for ( my $count = 1; $count &lt;= 10; $count++) {
          my $t = threads-&gt;new(\&amp;sub1, $count);
          push(@threads,$t);
  }   
  foreach (@threads) {
          my $num = $_-&gt;join;
          print "done with $num\n";
  }   
  print "End of main program\n";
  
  sub sub1 {
          my $num = shift;
          print "started thread $num\n";
          sleep $num;
          print "done with thread $num\n";
          return $num;
  }   
  </code></pre>
  
  <p>Source: <a href="https://wiki.bc.net/atl-conf/pages/viewpage.action?pageId=20548191" rel="nofollow">https://wiki.bc.net/atl-conf/pages/viewpage.action?pageId=20548191</a></p>
  
  <p>I searched some hours now but did not find an example, how to do this. Would be nice when someone could give me a hint how to start.</p>
  
  <p>Thank you.</p>