if ( $thr->tid && !threads::equal( $thr, th +reads->self ) ) { ##########Not Sure ####### foreach my $client ( keys %sharedHash ) { print " client $client\n"; my $thrnew = thread->new( \&compare, $client ); $sema->up; } #### #!/usr/bin/perl use strict; use threads; use threads::shared; use Time::HiRes qw (usleep); use POSIX; my @clientList = qw(client1 client2 client3 client4 client5 client6 client7 client8 client9 client10 client11 client12); my @dmServers = qw(A B C); my @queryPath = qw(AA BB CC DD EE FF); my @shared; my $queryIndex = 0; my $maxnoofThreads = 3; my %sharedHash : shared; for ( my $i = 0 ; $i < @dmServers ; $i++ ) { my $cpid = fork(); die unless defined $cpid; if ( !$cpid ) { # This is the child # my $wait = int rand 10; # sleep $wait; callChild( $queryPath[ $queryIndex++ ], $dmServers[$i], $i ); #print "Child $$ exiting after seconds\n"; exit; } } # Just parent code, after this while ( ( my $cpid = wait() ) != -1 ) { #print "Waited for child $cpid\n"; #print " length of array shared",length(@shared); } #print "Parent Exiting\n"; sub callChild { my $query = shift; my $dmserver = shift; my $i = shift; my %shash; #print " query fired $query on dmserver $dmserver index $i\n"; my @clientList_temp = @clientList; for ( my $index = $i ; $index < @queryPath ; $index = $index + @dmServers ) { print "process $$ query at $index is ", $queryPath[$index], "\n"; #prepare all of the threads for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { share $shash{$$}{$a}{'data'}; $shash{$$}{$a}{'data'} = undef; share $shash{$$}{$a}{'go'}; $shash{$$}{$a}{'go'} = 0; share $shash{$$}{$a}{'done'}; $shash{$$}{$a}{'done'} = 0; my @temp = splice( @clientList_temp, 0, ceil( @clientList / $maxnoofThreads ) ); $shash{$$}{$a}{'thread'} = threads->new( \&worker, $a, \@temp, $queryPath[$index], $$ ); print "pid: $$ tid is: ", $shash{$$}{$a}{'thread'}->tid,"\n"; } #launch all of the threads for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { print "launching pid:$$ tid: $a\n"; $shash{$$}{$a}{'go'} = 1; } #now wait on them to finish for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { $shash{$$}{$a}{'thread'}->join; } } sub worker { my $thr_num = shift; my $myClientArrayRef = shift; my $query = shift; my $pid = shift; WORKER_LOOP: while (1) { #wait for $go_control if ( $shash{$pid}{$thr_num}{'go'} ) { #and $go) { for ( my $i=0 ; $i < @$myClientArrayRef ; $i++ ) { print " $$myClientArrayRef[$i] client \n"; $shash{$pid}{$thr_num}{'data'} = "$$myClientArrayRef[$i]"; sleep 1; shift @$myClientArrayRef; } $shash{$pid}{$a}{'done'} = 1; #print "done\n"; last WORKER_LOOP; } else { usleep 5000; #essential sleep or will flog cpu waiting on while loop } # sleep until awakened } #end WORKER_LOOP return; } sub compare { my $client = shift; print " hash value ", $sharedHash{$client}, "\n"; } }