in reply to Re^7: Execute a sub in the background
in thread Execute a sub in the background

Ok i understand. So is there a way to share a variable or maybe to sent a reference to the sub.

What i want is to have a flag in the main process when its value is one the cycle in the thread to end. How can i do this. I checked the threads::schared but honestly i wasn't able to do anything. So if you could help me whit some example code it would be great

Replies are listed 'Best First'.
Re^9: Execute a sub in the background
by chessgui (Scribe) on Feb 01, 2012 at 16:49 UTC
    Experts will tell you to use Thread::Queue. I have not much experience with queues. However this is what you can achieve with shared variables:
    use threads; use threads::shared; my ($global):shared; my $handler=threads->create(\&handler)->detach; do { print "Enter a value for shared global variable ( x = exit ): ";ch +omp($global=<>); sleep(3); } while($global ne 'x'); sub handler { my $global_old=$global; while(1) { if($global ne $global_old) { print "User changed the global variable to '$global'.\n"; $global_old=$global; } sleep(1); } }