"yield" doesnt work

S

sangeetha

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
 
B

Ben Morrow

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top