Reputation: 3172
I think i shouldn't be getting this error, but since I am getting it, I am obviously wrong. The situation is very simple: Share one variable between N threads and have them increment it by one.
The code is pretty short and straight forward:
use threads;
use threads::shared;
use strict;
sub Handler()
{
my $shared_var : shared = 1000;
my @threads;
push @threads, thread->new(\&PrintGreeting);
$_->join for @threads;
}
sub PrintGreeting()
{
$shared_var++;
print "hello, world $shared_var \n";
}
Handler();
The error I am getting is:
Global symbol "$shared_var" requires explicit package name at /home/path/tosource/program.pl line 15.
Line 15 is the line where I increment the variable.
I searched, but I couldn't find someone with the same error.
Upvotes: 0
Views: 148
Reputation: 22893
Just because you are using threads doesn't mean the rest of Perl's rules are thrown out of the window.
You define my $shared_var
in Handler
but try to use it in PrintGreeting
. That wouldn't work with a normal variable and it won't work with one shared between threads either. Basic variable scoping.
Perhaps you meant to decalre a package-level (global) variable instead?
Upvotes: 1