enchanter has asked for the wisdom of the Perl Monks concerning the following question:
------------------------------------------------------------------------#!C:\Perl\bin\perl -w $|++; print "Content-type: text/html\n\n"; # use strict; use warnings; use Time::HiRes; use threads; use threads::shared; my ($doc_top, $doc_middle, $doc_bottom); $doc_top = "<html>\n"; $doc_top .= "<head>\n"; $doc_top .= "<script>\n"; $doc_top .= "function set(id,text) {\n"; $doc_top .= " document.getElementById(id).innerText = text\n"; $doc_top .= "}\n"; $doc_top .= "</script>\n\n"; $doc_top .= "</head>\n"; $doc_top .= "<body>\n\n"; $doc_top .= "<table cellSpacing=\"0\" cellPadding=\"0\" width=\"10%\" +border=\"0\">\n"; $doc_top .= "\t<tr>\n"; $doc_top .= "\t\t<td>\n"; $doc_top .= "\t\t\t<table width=\"100%\">\n"; #$doc_top .= "\t\t\t\t<tr>\n"; $doc_middle =''; #$doc_bottom .= "\t\t\t\t</tr>\n"; $doc_bottom .= "\t\t\t</table>\n"; $doc_bottom .= "\t\t</td>\n"; $doc_bottom .= "\t</tr>\n"; $doc_bottom .= "</table>\n\n"; #$doc_bottom .= "</body>\n"; #$doc_bottom .= "</html>\n"; for ($j = 0; $j <= 22; $j++) { $doc_middle .= "\t\t\t\t<tr>\n"; for ($i = 0; $i <= 2; $i++) { $doc_middle .= "\t\t\t\t\t<td id='cell$i$j' bgColor=\"#eeeeee\" al +ign=\"center\"> </td>\n"; } $doc_middle .= "\t\t\t\t</tr>\n"; } print $doc_top.$doc_middle.$doc_bottom; our (@rv, @pauses, @letters) : shared; #@rv[0,1,2, 3,4,5, 6, 7, 8] = (2, 5, 5,<br> @rv[0,1,2, 3,4,5, 6, 7, 8] = (int(rand(4))+2, int(rand(4))+2, int( +rand(4))+2, int(rand(4))+2, int(rand(4))+2, int( +rand(4))+2, int(rand(4))+2, int(rand(4))+2, int( +rand(4))+2); #@pauses[0,1,2, 3,4,5, 6, 7, 8] = (0.325543212890625, 0.3333251953 +125, 0.345562744140625,<br> @pauses[0,1,2, 3,4,5, 6, 7, 8] = ((rand(1))+.3, (rand(1))+.3, (ran +d(1))+.3, (rand(1))+.3, (rand(1))+.3, (ran +d(1))+.3, (rand(1))+.3, (rand(1))+.3, (ran +d(1))+.3); @letters[0,1,2, 3,4,5, 6,7,8] = ('a','b','c', 'd','e','f', 'g' +,'h','i'); my $cur_right_value; my $cur_pause; my $cur_letter; for($f = 0; $f <= 2; $f++) { $cur_right_value = @rv[$f];# print $cur_right_value; print "<br>\n"; $cur_pause = @pauses[$f]; # print $cur_pause; print "<br>\n"; $cur_letter = @letters[$f];# print $cur_letter; print "<br>\n"; print "for current letter <b>$cur_letter</b> repetitions <b>".($cur_ +right_value+1)."</b> via pause <b>$cur_pause</b> sec<br>\n"; $thr = threads->new(sub { my $cur_var = 0; for($cur_var = $cur_var; $cur_var <= $cur_right_value; $cur_var++) { print "<script>set('cell$f$cur_var', '$cur_letter')</script>\n"; if(Time::HiRes::sleep($cur_pause)) { lock(@rv); lock(@pauses); lock(@letters); } if($cur_var == $cur_right_value) { # print "<b>\$f = $f</b><br>\n"; $cur_right_value = @rv[0]+$cur_var; $cur_pause = @pauses[0]; $cur_letter = @letters[0]; shift @rv; # print @rv; print "<br>\n"; shift @pauses; # print @pauses; print "<br>\n"; shift @letters;# print @letters; print "<br>\n"; } } }); } shift @rv; shift @rv; shift @rv; # print @rv; print + "<br>\n"; shift @pauses; shift @pauses; shift @pauses; # print @pauses; print + "<br>\n"; shift @letters; shift @letters; shift @letters;# print @letters; print + "<br>\n"; $thr->join; print "@letters<br>\n"; print "<br>\n"; print "</body>\n"; print "</html>\n";
------------------------------------------------------------------------#!C:\Perl\bin\perl -w print "Content-type: text/html\n\n"; use warnings; use Time::HiRes; use threads; use threads::shared; $doc_top = "<html>\n"; $doc_top .= "<head>\n"; $doc_top .= "<script>\n"; $doc_top .= "function set(id,text) {\n"; $doc_top .= " document.getElementById(id).innerText = text\n"; $doc_top .= "}\n"; $doc_top .= "</script>\n\n"; $doc_top .= "</head>\n"; $doc_top .= "<body>\n\n"; $doc_top .= "<table style=\"font: 8pt Verdana, Arial, Helvetica, Sans- +serif; line-height:8pt;\" cellSpacing=\"1\" cellPadding=\"2\" width=\ +"21%\" border=\"1\">\n"; $doc_top .= "\t<tr>\n"; $doc_top .= "\t\t<td>\n"; $doc_middle =''; $doc_bottom .= "\t\t</td>\n"; $doc_bottom .= "\t</tr>\n"; $doc_bottom .= "</table>\n\n"; for ($j = 0; $j <= 32; $j++) { $doc_middle .= "\t\t\t<tr>\n"; for ($i = 0; $i <= 2; $i++) { $doc_middle .= "\t\t\t\t<td width=\"10%\" id='cell$i$j' bgColor=\" +#eeeeee\" align=\"center\"> </td>\n"; } $doc_middle .= "\t\t\t</tr>\n"; } print $doc_top.$doc_middle.$doc_bottom; print "<font style=\"font: 8pt Verdana, Arial, Helvetica, Sans-serif; +line-height:8pt;\">\n"; $|++; my $threads = 3; my $count : shared = $threads; my @threads; my @params = ( {id => '0', up => 8, to => 0.5, char => 'a'}, {id => '1', up => 5, to => 0.2, char => 'b'}, {id => '2', up => 11, to => 0.3, char => 'c'}, {id => '3', up => 6, to => 1.1, char => 'd'}, {id => '4', up => 2, to => 0.5, char => 'e'}, {id => '5', up => 9, to => 0.6, char => 'f'}, {id => '6', up => 3, to => 0.7, char => 'g'}, {id => '7', up => 5, to => 0.8, char => 'h'}, {id => '8', up => 6, to => 0.9, char => 'i'} ); sub fun { my $arg = shift; $count -= 1; my $cur_var = 0; for($cur_var = $cur_var; $cur_var <= $arg->{up}; $cur_var++) { redo if $count; print "<script>set('cell$arg->{id}$cur_var', '$arg->{char}')</scri +pt>\n"; Time::HiRes::sleep($arg->{to}); } } foreach(1..$threads) { push @threads, threads->new(\&fun, shift @params); } foreach(1..$threads) { my $thid = shift @threads; $thid->join; } print "</body>\n"; print "</html>\n";
Edit by castaway - added readmore tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to share array of hashes among several threads?
by mugwumpjism (Hermit) on May 24, 2005 at 01:02 UTC | |
|
Re: how to share array of hashes among several threads?
by BrowserUk (Patriarch) on May 24, 2005 at 02:19 UTC | |
|
Re: how to share array of hashes among several threads?
by kwaping (Priest) on May 24, 2005 at 02:31 UTC | |
|
Re: how to share array of hashes among several threads?
by zentara (Cardinal) on May 24, 2005 at 10:05 UTC | |
by BrowserUk (Patriarch) on May 25, 2005 at 06:25 UTC |