I have recently started experimenting with threads and have the following problem. Whenever I try and share a scalar ref using a queue I can't access that reference in other parts of my code by storing it in a global variable. The problem is illustrated by the code below.
P.S There is a reason I am not passing the reference directly to the function instead (even though this does seem to work)#!/usr/bin/env perl use strict; use threads; use Thread::Queue; my $q=Thread::Queue->new; my $thr=async { my $progref; sub ch { print "In ch \$progref=$progref\n"; #progref is still undefine +d (why?) $$progref=90; } $progref=$q->dequeue; #This seems to create a local $progref, but +it shouldn't! print "In main part of thread \$progref=$progref\n"; $$progref=30; #This is applied ch(); #This is ignored }; my $prog :shared; $q->enqueue(\$prog); sleep 2; print "$prog\n"; #Prints 30, not 90 $q->end; $thr->join;
In reply to Threads and Shared Variables by hermes1908
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |