I wrote a simple proxy scanner application using threads. However I can only test it on Windows as of now because my Linux server is still packed up. The issue is the main thread exits before all worker threads are joined despite the call to join at the end of the main thread. I am not sure if this is an issue with Windows or the application itself.

#!/usr/bin/perl -w use strict; use Thread; use LWP::UserAgent::ProxyAny; my $NumPerThread = 6; open(PROXY, "<", "proxies.txt") || die "No proxy list found!\n"; my @proxies = <PROXY>; close(PROXY); my ($thread, $cnt, @group); for (@proxies) { push(@group, $_); $cnt++; if ($cnt >= $NumPerThread) { $thread = new Thread \&process, @group; undef @group; undef $cnt; } } if (scalar @group) { $thread = new Thread \&process, @group; undef @group; } $thread->join; print "died with error $@\n" if ($@); print "Main Thread Exiting!\n"; sub process { my @section = @_; for (@section) { chomp(); my $ua = LWP::UserAgent::ProxyAny->new; $ua->set_proxy_by_name($_); $ua->timeout(2); my $response = $ua->get('http://www.google.com'); if ($response->is_success) { print "Connection using $_ was successfull!\n"; open(GOOD, ">>", "good_proxies.txt") || die "Unable to open good proxy list for writing: ($!)\n"; flock(GOOD, 2); print GOOD $_,"\n"; close(GOOD); }else{ print "Connection using $_ failed!\n"; } } }

Is there something I have missed here? I have tried calling join after each thread creation and this allows each thread to finish however it blocks before continueing on to the next one due to the blocking nature of join(). Using that method is just as slow as not using threads at all.

In reply to Main thread exist before all worker threads? by Elijah

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.