Skip to content

Instantly share code, notes, and snippets.

@BrianFehrle
Created August 3, 2012 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianFehrle/3252063 to your computer and use it in GitHub Desktop.
Save BrianFehrle/3252063 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use threads;
my %tracks = ();
$tracks{'106019300'} = 'I are thread 1';
$tracks{'106019299'} = 'I are thread 2';
my %threadLst = ();
foreach my $key ( keys %tracks )
{
$threadLst{$key} = threads->create(\&ingest_track, "$key", "$tracks{$key}");
}
sub ingest_track {
my ($market_id, $trackname) = @_;
my $rand = rand() * 20;
print("In the thread var is $market_id, $trackname, rand [$rand]\n");
sleep($rand);
}
sleep(1);
my @threads = threads->list();
for my $thread (@threads) {
print "thread [$thread] is a thread...\n";
}
my $thread_count = threads->list();
print "There are $thread_count threads running\n";
my @running = threads->list(threads::running);
for my $thread (@running) {
print "thread [$thread] is running...";
}
my @joinable = threads->list(threads::joinable);
for my $thread (@joinable) {
print "thread [$thread] is joinable...\n";
}
while ($thread_count = threads->list() > 0) {
foreach my $key ( keys %tracks )
{
print "DEBUG: Checking to see if $key is joinable\n";
if ($threadLst{$key}->is_joinable()) {
print "Joining $key\n";
$threadLst{$key}->join();
delete $tracks{$key};
delete $threadLst{$key};
} else {
print "Ok, so didn't join thread with id [$key]....\n";
}
}
print "\n";
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment