Problem starting a new thread from another thread

Discussion in 'Perl Misc' started by tzhong98-101@yahoo.com, Jul 26, 2006.

  1. Guest

    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
     
    , Jul 26, 2006
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > 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

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, Jul 26, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 26, 2006
    #3
  4. Guest

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

    Tim
    wrote:
    > wrote:
    > > 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
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 3, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lenny
    Replies:
    3
    Views:
    2,469
    Lenny
    Jul 8, 2003
  2. Adrian Bezzina
    Replies:
    0
    Views:
    404
    Adrian Bezzina
    Jan 18, 2005
  3. Maziar Aflatoun
    Replies:
    1
    Views:
    507
    =?Utf-8?B?UGF1bA==?=
    Jan 22, 2005
  4. Doug Miller
    Replies:
    3
    Views:
    791
    David Hilsee
    Feb 14, 2005
  5. Richard Lamboj

    starting a thread in a nother thread

    Richard Lamboj, Jan 27, 2010, in forum: Python
    Replies:
    7
    Views:
    284
Loading...

Share This Page