in reply to Should I use threads? Perl/DHCP/Radius

I think your main problem is that you are using async. It is useful for small blocks of code , but once the subroutine gets to your level of complexity, it is better to name it and use threads->create.

Once you make that change, it then becomes clear that you have a third option to immediately calling either join() or detach(). You can simply save the thread handles and defer that until later. See how you get on with this:

#!/usr/bin/perl use strict; use LWP::UserAgent; use HTTP::Request; use threads; use threads::shared; use strict; my %clients :shared; sub doit{ my $mac = shift; my $macAuthURL = "https://XXXXXXXX/api/macCheck/"; my ($request, $ua, $response, $respcon, $rval); my ($returnkey,$returnval); print "MAC: ".$mac." Contacting $macAuthURL$mac.\n"; print "This is ".threads->tid()."\n"; # Wrap call to API with eval eval { local $SIG{ALRM} = sub { die "Alarm\n" }; alarm(16); my $macr = $macAuthURL.$mac; $request = HTTP::Request->new(GET => $macr); $ua = LWP::UserAgent->new(); $response = $ua->request($request); $respcon = $response->content(); alarm(0); }; # We check the response object's status if($response->status_line =~ /^500/ ){ print "MAC: ".$mac." Network Delay/Error."; { lock (%clients); $clients{$mac} = 0; } } print "MAC: ".$mac." Server response: $respcon \n\n\n" ; # All OK - Ensure that sent mac is returned back on success. if( $response->{_rc} == 200 ){ $response->{_content} =~ s/[\{\} \"]//g; ( $returnkey, $returnval ) = split(/:/,$response->{_content},2 +); print "MAC: ".$mac." returned with status: $response->{_rc} " +; if( $mac eq uc($returnval) ){ { lock (%clients); $clients{$mac} = 1; } } else{ # Malformed URL { lock (%clients); $clients{$mac} = 0; } } } else{ { lock (%clients); $clients{$mac} = 0; } } sleep 1; } # A list of MACs extracted from DHCP leases. my @maclist = qw ( 7D:6D:62:C6:B4:3D 01:12:79:3E:14:2E 80:27:E4:EA:60:74 E8:06:88:7F:8C:83 01:26:08:E8:B6:5D 34:15:9E:5D:E6:49 03:1E:64:CE:25:88 01:0C:29:3F:67:1B 01:22:69:76:5D:F4 64:B9:E8:10:BF:20 01:1D:FE:D4:7F:E0 ); my @threads; foreach my $mac (@maclist){ push @threads, threads->create( \&doit, $mac ); } $_->join for @threads;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy