sasikumar has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a very simple issue with perl threads and i am not sure where i am going wrong. Here is my code.
use threads; threads->create(\&start(),""); threads->create(sub {print "started thread\n"},""); threads->join(); sub start() { print "processing ...\n"; while(1) { sleep(5); print "test\n"; threads::yield(); } }
The strange part is i never see the print message for "started thread" It seems like program is stuck with the first thread.... an idea? Thanks SasiKumar

Replies are listed 'Best First'.
Re: Perl threads - just stuck
by sasikumar (Monk) on Mar 13, 2009 at 07:25 UTC
    Was just stupid...... i think i need a break now ;)
    threads->create(\&start(),"");
    I am calling the start function instead of passing the reference.... Here is the correct way of doing it.
    threads->create(\&start,"");
    Now it works like a charm... Man that was easy.
    Thanks SasiKumar
Re: Perl threads - just stuck
by Anonymous Monk on Mar 13, 2009 at 06:14 UTC
    perlthrtut : It is important to remember that yield() is only a hint to give up the CPU, it depends on your hardware, OS and threading libraries what actually happens. On many operating systems, yield() is a no-op. Therefore it is important to note that one should not build the scheduling of the threads around yield() calls. It might work on your platform but it won't work on another platform.
      In this case, it doesn't seem that yield would matter. If the start thread is sleeping for five seconds on every loop, there would be no reason that the other thread wouldn't get scheduled. And as you can see in sasikumar's reply, scheduling wasn't the issue at all.