[pm-h] Binding variables to a reference

Robert Boone robo4288 at gmail.com
Sun Dec 21 13:47:41 PST 2008


I may have misunderstood and if so please disregard.

But in my understanding of perl threads and the forks module is that
you don't pass shard variables as arguments. You just declare them
before creating the thread and use them because they're in scope. Here
is a bad example:

#!/usr/bin/perl

use strict;
use threads;
use threads::shared;

my $num : shared;
my $end : shared;
$num = 0;
$end = 0;

sub Boss {
  lock $num;
  $num = 5;
  sleep 4;
  lock $end;
  $end = 1;
  print "Boss: DONE!\n";
}

sub Worker {
    while (!$end) {
	  sleep 1;
	  print $num. "\n";
    }
	print "Worker: DONE!\n";
}

my @hold;
push @hold, threads->new( \&Boss );
push @hold, threads->new( \&Worker );

$_->join foreach (@hold);


More information about the Houston mailing list