CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Problem passing variables to thread

 



barteled
New User

Sep 15, 2005, 5:43 PM

Post #1 of 1 (956 views)
Problem passing variables to thread Can't Post

I'm having difficulty passing variables to threads. I've recently compiled 5.8.7 with threads enabled and I am using the new ithreads. When running code as follows:

[code]
use Thread qw(async);
async {
while(1) {
print 1;
}
};

while (1) {
print 2;
}[/code]

The code works as expected and prints both 1's and 2's. But whenever I try to use a parent's variables in children processes I run into problems. For example from the camel book:
[code]
use Thread qw(async);
use Thread::Queue;

my $Q = Thread::Queue->new();
async {
while (defined($datum = $Q->dequeue)) {
print $datum;
}
};

$Q->enqueue(12);
$Q->enqueue("A");

while(1) {
print 1;
}
[/code]
12 and A are never printed, just 1; And if I try printing $Q from both inside and outside the async block, they reference to different objects. I was under the impression, and I believe the O'Reilly book is to, that children have direct access to variables that are created in the parents before the thread is initiated. Am I wrong in this assumption? Is there a compiler flag that I missed to allow this kind of variable scope? If this code is wrong any insights on how to access parent process objects from within threads would be greatly appreciated.

I'm using linux (kernel 2.6.11). I'm not sure what other information about my system configuration could pertain to this problem. Please ask if more info is needed.

Thanks in advance for any help given.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives