P
poohc1234
Hi,
I have copied the code from perlthrtut.html with the following:
use Thread qw(yield);
new Thread \&thread_sub, 1;
new Thread \&thread_sub, 2;
new Thread \&thread_sub, 3;
new Thread \&thread_sub, 4;
sub sync_sub :locked {
my $CallingThread = shift @_;
print "In sync_sub for thread $CallingThread\n";
sleep 3;
print "Leaving sync_sub for thread $CallingThread\n";
}
sub thread_sub {
my $ThreadID = shift @_;
print "Thread $ThreadID calling sync_sub\n";
sync_sub($ThreadID);
print "$ThreadID is done with sync_sub\n";
}
But it doesn't work. The result I get was:
Thread 1 calling sync_sub
In sync_sub for thread 1
Thread 2 calling sync_sub
In sync_sub for thread 2
A thread exited while 6 threads were running.
Thread 3 calling sync_sub
In sync_sub for thread 3
Thread 4 calling sync_sub
In sync_sub for thread 4
So I changed the code to be called with join:
for (0..4)
{
$Child[$_] = threads->new(\&thread_sub, $_);
}
for (0..4)
{
$Child[$_]->join;
}
and this still doesn't work because all the threads enters the
subroutine at the same time. I know this because the code exited after
3 seconds, instead of 5 clients * 3 seconds. Though I don't understand
why 2 threads that were still executing with the code exits.
This is the result:
Thread 0 calling sync_sub
In sync_sub for thread 0
Thread 1 calling sync_sub
In sync_sub for thread 1
Thread 2 calling sync_sub
In sync_sub for thread 2
Thread 3 calling sync_sub
In sync_sub for thread 3
Thread 4 calling sync_sub
In sync_sub for thread 4
Leaving sync_sub for thread 0
Leaving sync_sub for thread 1
Leaving sync_sub for thread 2
Leaving sync_sub for thread 3
Leaving sync_sub for thread 4
A thread exited while 2 threads were running.
Very confused.
Thanks,
jiac
I have copied the code from perlthrtut.html with the following:
use Thread qw(yield);
new Thread \&thread_sub, 1;
new Thread \&thread_sub, 2;
new Thread \&thread_sub, 3;
new Thread \&thread_sub, 4;
sub sync_sub :locked {
my $CallingThread = shift @_;
print "In sync_sub for thread $CallingThread\n";
sleep 3;
print "Leaving sync_sub for thread $CallingThread\n";
}
sub thread_sub {
my $ThreadID = shift @_;
print "Thread $ThreadID calling sync_sub\n";
sync_sub($ThreadID);
print "$ThreadID is done with sync_sub\n";
}
But it doesn't work. The result I get was:
Thread 1 calling sync_sub
In sync_sub for thread 1
Thread 2 calling sync_sub
In sync_sub for thread 2
A thread exited while 6 threads were running.
Thread 3 calling sync_sub
In sync_sub for thread 3
Thread 4 calling sync_sub
In sync_sub for thread 4
So I changed the code to be called with join:
for (0..4)
{
$Child[$_] = threads->new(\&thread_sub, $_);
}
for (0..4)
{
$Child[$_]->join;
}
and this still doesn't work because all the threads enters the
subroutine at the same time. I know this because the code exited after
3 seconds, instead of 5 clients * 3 seconds. Though I don't understand
why 2 threads that were still executing with the code exits.
This is the result:
Thread 0 calling sync_sub
In sync_sub for thread 0
Thread 1 calling sync_sub
In sync_sub for thread 1
Thread 2 calling sync_sub
In sync_sub for thread 2
Thread 3 calling sync_sub
In sync_sub for thread 3
Thread 4 calling sync_sub
In sync_sub for thread 4
Leaving sync_sub for thread 0
Leaving sync_sub for thread 1
Leaving sync_sub for thread 2
Leaving sync_sub for thread 3
Leaving sync_sub for thread 4
A thread exited while 2 threads were running.
Very confused.
Thanks,
jiac