hermes1908 has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads and Shared Variables
by BrowserUk (Patriarch) on Jul 04, 2013 at 19:31 UTC | |
by hermes1908 (Novice) on Jul 04, 2013 at 20:40 UTC | |
by BrowserUk (Patriarch) on Jul 04, 2013 at 21:11 UTC | |
by hermes1908 (Novice) on Jul 04, 2013 at 22:18 UTC | |
by BrowserUk (Patriarch) on Jul 04, 2013 at 22:35 UTC | |
by Arunbear (Prior) on Jul 05, 2013 at 11:25 UTC | |
|
Re: Threads and Shared Variables
by roboticus (Chancellor) on Jul 04, 2013 at 19:42 UTC | |
|
Re: Threads and Shared Variables
by Preceptor (Deacon) on Jul 04, 2013 at 19:31 UTC |