title: Perl Multi Threaded Program crashes Sporadically
body: <p>I have written a program in Perl which makes use of multi threading. I am using this program to understand how multi threading is implemented in Perl.</p>
  
  <p>First a brief overview of what the program intends to do: It will read a list of URLs from a text file, one at a time. For each URL, it will call a subroutine (passing the URL as a parameter) and send an HTTP HEAD request to it. Once it receives the HTTP Response headers, it will print the Server Header field from the response.</p>
  
  <p>For each URL, it starts a new thread which calls the above subroutine.</p>
  
  <p>The problem: The main issue is that the program crashes intermittently at times. It runs properly the other times. It appears to be unreliable code and I am sure there is a way to make it work reliably.</p>
  
  <p>The code:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use threads;
  use WWW::Mechanize;
  no warnings 'uninitialized';
  
  open(INPUT,'&lt;','urls.txt') || die("Couldn't open the file in read mode\n");
  
  print "Starting main program\n";
  
  my @threads;
  
  while(my $url = &lt;INPUT&gt;)
  {
      chomp $url;
      my $t = threads-&gt;new(\&amp;sub1, $url);
      push(@threads,$t);
  }
  
  foreach (@threads) {
      $_-&gt;join;
  }
  
  print "End of main program\n";
  
  sub sub1 {
      my $site = shift;
      sleep 1;
      my $mech = WWW::Mechanize-&gt;new();
      $mech-&gt;agent_alias('Windows IE 6');
  
      # trap any error which occurs while sending an HTTP HEAD request to the site
      eval{$mech-&gt;head($site);};
      if($@)
      {
          print "Error connecting to: ".$site."\n";
      }
  
      my $response = $mech-&gt;response();
  
      print $site." =&gt; ".$response-&gt;header('Server'),"\n";
  }
  </code></pre>
  
  <p>Questions:</p>
  
  <p>How can I make this program work reliably and what is the reason for sporadic crashes? </p>
  
  <p>What is the purpose of calling the join method of the thread object?</p>
  
  <p>As per the documentation at the link below, it will wait for the thread execution to complete. Am I invoking the join method correctly?</p>
  
  <p><a href="http://perldoc.perl.org/threads.html">http://perldoc.perl.org/threads.html</a></p>
  
  <p>If there are any good programming practices which I must include in the above code, please let me know. </p>
  
  <p>Do I need to call sleep() exclusively in the code or is it not required? </p>
  
  <p>In C, we would call Sleep() after calling CreateThread() to begin the execution of the    thread.</p>
  
  <p>Regarding the crash: When the above Perl code crashes unexpectedly and sporadically, I get the error message: "Perl command line interpreter has stopped working"</p>
  
  <p>Details of the crash:</p>
  
  <pre><code>Fault Module Name:  ntdll.dll
  Exception Code: c0000008
  </code></pre>
  
  <p>The above exception code corresponds to: STATUS_INVALID_HANDLE</p>
  
  <p>Maybe this corresponds to invalid handle of the thread.</p>
  
  <p>Details of my Perl Installation:</p>
  
  <pre><code>Summary of my perl5 (revision 5 version 14 subversion 2) configuration:
  
  Platform:
  osname=MSWin32, osvers=5.2, archname=MSWin32-x86-multi-thread
  useithreads=define
  </code></pre>
  
  <p>Details of the OS: Win 7 Ultimate, 64-bit OS.</p>
  
  <p>Hope this information would be sufficient to find the root cause of the issue and correct the code.</p>