Problem starting a new thread from another thread

T

tzhong98-101

I am trying to do some threading with perl, and my simple script just
refuses to work. Basically I created a new thread (T1), and then
attempted to create another one (T2) from within T1, which failed
silently. Could anyone tell me why that is?

======== The sample code ========

#!C:/perl/bin/perl.exe

use strict;
use IO::Socket;
use Thread;
use Config;
$Config{useithreads} or die "Recompile Perl with threads to run this
program.";


sub foo2 {
sleep(5);
print "foo2";
print "T2 is done";
}

sub foo1 {
sleep(2);
print "foo1";
my $t2 = new Thread \&foo2;
print "T2 is launched";
}

$\ = "\n";
my $t1 = new Thread \&foo1;
print "T1 is launched";

===== End of sample code ====

The Output:

T1 is launched
foo1


Apparently thread T2 is not even started. What's wrong?

Thanks,
Tim
 
B

Ben Morrow

Quoth (e-mail address removed):
I am trying to do some threading with perl, and my simple script just
refuses to work. Basically I created a new thread (T1), and then
attempted to create another one (T2) from within T1, which failed
silently. Could anyone tell me why that is?

======== The sample code ========

#!C:/perl/bin/perl.exe

use strict;
use IO::Socket;
use Thread;

Thread.pm is for 5005threads. I believe it does back-compat with
ithreads, but you should be using threads.pm nowadays.
use Config;
$Config{useithreads} or die "Recompile Perl with threads to run this
program.";

There's no need for this. threads.pm will croak if perl doesn't have
threads enabled (if you want threads iff they're there, you need to do
something like

use constant HAVE_THREADS => eval {
require threads;
threads->import;
1; # work around a bug in (IIRC) 5.8.1
};

and then test HAVE_THREADS (untested, as my perl isn't threaded)).

Ben
 
X

xhoster

I am trying to do some threading with perl, and my simple script just
refuses to work. Basically I created a new thread (T1), and then
attempted to create another one (T2) from within T1, which failed
silently. Could anyone tell me why that is?

Do you get any message like this:
A thread exited while 2 threads were running.
If so, you should either join the threads or just put a sleep in the main
code to give the threads time to finish.
======== The sample code ========

#!C:/perl/bin/perl.exe

use strict;
use IO::Socket;
use Thread;

You should probably use threads;, not use Thread;.

Xho
 
T

tzhong98-101

I changed Thread to threads, and it works great. Thanks guys for your
help!

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top