"yield" doesnt work

Discussion in 'Perl Misc' started by sangeetha, Nov 18, 2003.

  1. sangeetha

    sangeetha Guest

    Hello,

    Yesterday, i have started reading thread model programming in perl. (
    using Programming PERL by Larry wall, Tom, Jon). The following
    programming has been given to explain the LOCK CONCEPT and given that
    output of this program is "2". But, i'm getting "0" only. Any help y
    it is?

    I'm using "v5.8.0 built for i386-linux-thread-multi" version of perl.

    ##############################################
    use Thread qw/async yield/;
    my $var=0;
    sub abump {
    if ( $var == 0 ){
    yield;
    $var++;
    }
    }

    my $t1 = new Thread \&abump;
    my $t2 = new Thread \&abump;

    for my $t ( $t1, $t2 ) { $t ->join; }
    print "var is $var\n";
    ##############################################

    If my understanding is wrong please explain. What is the exact use of
    "yield" in the thread model programming. Thanks a lot.

    Sangeetha
     
    sangeetha, Nov 18, 2003
    #1
    1. Advertising

  2. sangeetha

    Ben Morrow Guest

    (sangeetha) wrote:
    > Yesterday, i have started reading thread model programming in perl. (
    > using Programming PERL


    Perl

    > by Larry wall,


    Wall

    > Tom, Jon


    ....whom I believe also have surnames... :)

    > ). The following
    > programming has been given to explain the LOCK CONCEPT and given that
    > output of this program is "2". But, i'm getting "0" only. Any help y
    > it is?
    >
    > I'm using "v5.8.0 built for i386-linux-thread-multi" version of perl.
    >
    > ##############################################
    > use Thread qw/async yield/;


    Thread.pm is obsolete with 5.8. The new thread model uses threads.pm,
    and by default variables are *not* shared between threads: for this
    you need to use threads::shared. The stuff in the Camel about
    threading refers to the old, 5.005, thread model; most of the
    specifics have changed.

    > If my understanding is wrong please explain. What is the exact use of
    > "yield" in the thread model programming. Thanks a lot.


    As a second point, using yield guarantees you *nothing* about the
    order threads will run in. It is entirely possible for one thread to
    run all the way to completion (even if it uses yield) before the other
    has started. If you need to synchronise, you must use lock and the
    cond_wait etc. primitives. (Basically, under any decent timesharing OS
    yield is precisely useless: mltitasking will happen properly without
    it, and putting it in won't materially affect how it happens. Its use
    is on broken systems like Win98 which don't preemptively multitask and
    need prodding every now and then to give someone else a chance at a
    bit of CPU.)

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Nov 18, 2003
    #2
    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. Janaka Perera
    Replies:
    0
    Views:
    1,135
    Janaka Perera
    Oct 29, 2007
  2. Replies:
    1
    Views:
    324
    Gabriel Genellina
    Apr 22, 2008
  3. Replies:
    3
    Views:
    438
    alex23
    May 27, 2008
  4. Markus
    Replies:
    1
    Views:
    199
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    280
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page