note
BrowserUk
<p>It doesn't crash for me?
<readmore><code>
P:\test>perl -v
This is perl, v5.8.3 built for MSWin32-x86-multi-thread
(with 8 registered patches, see perl -V for more detail)
Copyright 1987-2003, Larry Wall
Binary build 809 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Feb 3 2004 00:28:51
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.
P:\test>type 367162.pl
#!/usr/bin/perl -w
use strict;
use threads;
$|=1;
sub mycmp { length($b) <=> length($a) }
sub do_one_thread {
my $kid = shift;
warn "kid $kid before sort\n";
my @list = ( 'x', 'yy', 'zzz', 'a', 'bb', 'ccc', 'aaaaa', 'z',
'hello', 's', 'thisisalongname', '1', '2', '3',
'abc', 'xyz', '1234567890', 'm', 'n', 'p' );
for my $j (1..1000) {
# This does not crash
# for my $k (sort { length($b) <=> length($a) } @list) {}
# Yet this does
for my $k (sort mycmp @list) {}
}
warn "kid $kid after sort\n";
}
sub do_threads {
my $nthreads = shift;
my @kids = ();
for my $i (1..$nthreads) {
my $t = threads->new(\&do_one_thread, $i);
warn "parent $$: continue\n";
push(@kids, $t);
}
for my $t (@kids) {
warn "parent $$: waiting for join\n";
$t->join();
warn "parent $$: thread exited\n";
}
}
# do_threads(1); # does not crash
do_threads(2); # crashes
P:\test>367162
parent 320: continue
kid 1 before sort
kid 1 after sort
parent 320: continue
parent 320: waiting for join
parent 320: thread exited
parent 320: waiting for join
kid 2 before sort
kid 2 after sort
parent 320: thread exited
</code></readmore>
<div class="pmsig"><div class="pmsig-171588">
<hr />
<font size=1 >
<div>Examine what is said, not who speaks.</div>
"Efficiency is intelligent laziness." -David Dunham<br />
"Think for yourself!" - [Abigail-II|Abigail] <br />
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - [tachyon]<br />
</font>
</div></div>
367162
367162