Hi There,
Can someone tell me why this is not enqueueing the hashs, and tell me the solution?
Here is the code:
#!/usr/bin/perl
use warnings;
use threads qw[ yield ];
use threads::shared;
use Thread::Queue;
use Mysql;
use constant NTHREADS => $ARGV[1];
$db = Mysql->connect('localhost', 'xxx', 'xxx', 'xxx');
%action = GetActionDetail($ARGV[0]);
%userData = GetUserDetail($action{'id_user'});
%companyData = GetCompanyDetail($userData{'id_company'});
my $pos :shared = 0;
$sql = "SELECT * FROM v_queue WHERE id=".$ARGV[0];
$query = $db->query($sql);
$total = $query->numrows;
if ($total>0){
my $Q = Thread::Queue->new;
my @threads = map threads->create( \&worker, $Q ), 1 .. NTHREADS;
sleep 0.5 while $Q->pending;
while (%queueData = $query->fetchhash){
my %item = ();
$item{'action'} = $action;
$item{'user'} = $userData;
$item{'company'} = $companyData;
$item{'queueData'} = $queueData;
$Q->enqueue( $item );
lock $pos;
}
$Q->enqueue( (undef) x NTHREADS );
$_->join for @threads;
}
sub GetActionDetail {
my $id = $_[0];
my $sql = "SELECT * FROM `actions` WHERE id='$id'";
my $query = $db->query($sql);
my $found = $query->numrows;
if ($found==1){
return $query->fetchhash;
} else {
return "false";
}
}
sub GetUserDetail {
my $id = $_[0];
my $sql = "SELECT * FROM `user` WHERE id='$id'";
my $query = $db->query($sql);
my $found = $query->numrows;
if ($found==1){
return $query->fetchhash;
} else {
return "false";
}
}
sub GetCompanyDetail {
my $id = $_[0];
my $sql = "SELECT * FROM `company` WHERE id='$id'";
my $query = $db->query($sql);
my $found = $query->numrows;
if ($found==1){
return $query->fetchhash;
} else {
return "false";
}
}
sub worker {
my $Q = shift;
my $tid = threads->tid;
while( $item = $Q->dequeue ) {
print "ITEM ID: ".$item{'action'}{'id'}."\n";
}
}