Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Multi-threading in 5.8.0

by DigitalKitty (Parson)
on Apr 19, 2003 at 23:55 UTC ( [id://251743]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all.

In the following code:
#!/usr/bin/perl -w use strict; use threads qw( yield ); my $thread1 = threads->new(\&sub1); my $thread2 = threads->new(\&sub2); my @data = $thread1->join; print "'Thread1' returned @data"; my @data2 = $thread2->join; print "'Thread2' returned @data2"; sub sub1 { yield; sleep 1; print "I'm a perl programmer!\n"; return "Threading in perl is fun!\n"; } sub sub2 { print "I like computing!\n"; return "Computing is cool!\n"; }


It seems I am unable to obtain different output ( thread1 always executes first even if I include the yield function inside of it ) unless I include a quantity of sleep time in sub1. Isn't that the purpose of 'yield' ( to give up it's CPU time to another thread )? This is my first exposure to perl threads so I apologize if this posting is overly simplistic.


Sample output without using sleep ( win98 - ActiveState 5.8.0 build ): C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>

Sample output with sleep and yield: C:\Perl\bin\hw>perl pg5.pl I like computing! *pause for 1 second* I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I like computing! I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I like computing! I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>
From what I understand, perl 6 is going to include an improved thread model. Very interesting indeed.

Thanks,
-Katie.

Replies are listed 'Best First'.
Re: Multi-threading in 5.8.0
by pfaut (Priest) on Apr 20, 2003 at 00:01 UTC

    yield will give up the CPU and cause the scheduler to pick a new current thread which might very well be the same thread that just called yield. You can't make assumptions about how threads will be scheduled. If you need to control which thread will execute when, you need to use locking mechanisms.

    90% of every Perl application is already written.
    dragonchild
Re: Multi-threading in 5.8.0
by JamesNC (Chaplain) on Apr 20, 2003 at 03:10 UTC
    I had a post on threads in 5.8 some time ago (Win32) on using threads to ping lots of servers... I really like using threads, but there are some documented problems with leaking scalars.. perhaps these will be fixed in 6.0 :) Here is the final code that worked there, it might be a useful learning example... looks like you are on the right track:
    use threads; use threads::shared; use strict; my $wait = 500; my @up: shared; my $th; my @childs; my $child; my @down: shared; my @list = qw ( www.apple.com www.perl.org www.ibm.com www.coke.com www.oracle.com www.hp.com ); foreach(@list){ push @childs, threads->create("ping","$_"); } foreach $child (@childs){ $child->join(); } print @up; print @down; sub ping { my $ip = shift; my @rv = `ping -n 1 -w $wait $ip`; foreach(@rv){ push @up, "$ip up\n" if /\(0% loss\)/; push @down, "$ip down\n" if /\(100% loss\)/; } }
Re: Multi-threading in 5.8.0
by halley (Prior) on Apr 20, 2003 at 13:14 UTC

    When it comes to testing a multi-threaded app, there is no substitute for getting a few hours on a multiple-processor system. All your tests which you ran on the single-processor machine are valid, but only a part of the testing you'll need to do. You'll soon discover the difference between empirical evidence and proven robustness.

    What's worse is that each new threading model can also affect borderline code. What worked on POSIX will have a race condition on green threads, or vice versa. Knowing your OS's threading model won't help very much from an application level: you just have to get it right, not merely apparently okay. It'll come from experience.

    --
    [ e d @ h a l l e y . c c ]

Re: Multi-threading in 5.8.0
by petesmiley (Friar) on Apr 21, 2003 at 15:58 UTC
    Also to top off what everyone has written, yield will behave as dictated by the platform it runs on. In some cases it might now do anything for you. From some of the work I have done, it takes some time to start a fresh thread. Your first thread might be finished by the time the second thread has actually started in which case a yield would be moot.

    If you are depending on the order in which your threads execute then you should carefully reconsider your design.

    smiles

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://251743]
Approved by djantzen
Front-paged by fsn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found