include '../variables.php'; ?>
echo($sLabName);?>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | use strict; use threads; use Thread::Queue; use threads::shared; # Create queue my $oQue = Thread::Queue->new; my $iThreadMax = 8; my $iTargetCnt; my @aTaskList = (); print "--------------------------------------------\n"; print "Collecting Random Numbers......\n"; my $iMax = 100000; for (my $i=0; $i<$iMax; $i++) { my $iRandNo = int(rand(100)+1); push(@aTaskList, $iRandNo); if ($i % int($iMax * 0.1) == 0) { print " ".$i."th Number = ".$iRandNo."\n"; } } # Data sharing between threads. Only single level is allowed for hashes. # Multiple-level hashes are not allowed. So, we use combined keys for multiple levels. my %hVal_by_ThreadGroup = (); share(%hVal_by_ThreadGroup); # to share data with other threads. # Adding Tasks to a Que print "--------------------------------------------\n"; print "Adding Tasks to a Que......\n"; print " Target enrolled in a que: "; foreach my $sCurTask (@aTaskList) { $iTargetCnt++; # print $sCurTask." "; $oQue->enqueue ($sCurTask); } print "\n"; if ($iTargetCnt < $iThreadMax ) { $iThreadMax = $iTargetCnt; } print " --------------\n"; print " Total No. of Threads: $iThreadMax\n"; print " --------------\n"; # Generate Threads and make them do something independently. for (my $i=0; $i<$iThreadMax; $i++) { # print " New Thread Count: ".$i." --> Generated!!\n"; $oQue -> enqueue(undef); # Main function that you like to handle the tasks after task split threads->new(\&funcDoIt, $i); } # Join up with the thread for normal finalization my @aObjTaskNames = threads->list(); foreach my $oCurTask (@aObjTaskNames) { # print "Joining Task: ".$oCurTask."\n"; $oCurTask->join(); } sub funcDoIt { # print " Function Started...............................\n"; my $iThreadCnt = shift; #($i, "a", "b", "c") --> $i my $sThread = sprintf("Thread_%02d", $iThreadCnt); my $iCnt = 0; while (my $sCurTask = $oQue->dequeue()) { $iCnt++; my $sCurGroup = ""; if ($sCurTask % 5 == 1) { $sCurGroup = "Group_A"; } elsif ($sCurTask % 5 == 2) { $sCurGroup = "Group_B"; } elsif ($sCurTask % 5 == 3) { $sCurGroup = "Group_C"; } elsif ($sCurTask % 5 == 4) { $sCurGroup = "Group_D"; } else { $sCurGroup = "Group_E"; } my $sCurKey = $sThread."==".$sCurGroup; $hVal_by_ThreadGroup{$sCurKey} = $hVal_by_ThreadGroup{$sCurKey}." ".$sCurTask; } print " Count of Numbers: ".$iCnt." / ".$sThread."\n"; } print "--------------------------------------------\n"; print "Rearranging data............................\n"; my %hVal_by_Group = (); my @aKeys = keys(%hVal_by_ThreadGroup); foreach my $sCurKey (@aKeys) { my @aTemp = split("==", $sCurKey); my $sCurGroup = $aTemp[1]; $hVal_by_Group{$sCurGroup} = $hVal_by_Group{$sCurGroup}." ".$hVal_by_ThreadGroup{$sCurKey}; } print "--------------------------------------------\n"; my @aGroups = keys(%hVal_by_Group); foreach my $sCurGroup (sort(@aGroups)) { my @aElement = split(" ", $hVal_by_Group{$sCurGroup}); print $sCurGroup.": Sum = ".funcArraySum(@aElement)." / Mean = ".funcMean(@aElement)." / Cnt = ".($#aElement + 1)."\n"; } sub funcArraySum { my @aData = @_; # Pre-Existing Array "@_" my $fSum = 0; foreach my $fCurVal (@aData) { $fSum += $fCurVal; } return $fSum; # Return a value / Optional } sub funcMean { my @aData = @_; # Pre-Existing Array "@_" my $fSum = 0; foreach my $fCurVal (@aData) { $fSum += $fCurVal; } my $iCnt = $#aData + 1; return $fSum/$iCnt; # Return a value } # Refer to the following link for more information. # https://perldoc.perl.org/Thread/Queue.html # https://perldoc.perl.org/threads/shared.html # https://perldoc.perl.org/5.8.8/threads/shared.html |