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.

In reply to Multi-threading in 5.8.0 by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.