Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^8: Execute a sub in the background

by egzoti4en (Initiate)
on Feb 01, 2012 at 16:23 UTC ( [id://951263]=note: print w/replies, xml ) Need Help??


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); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://951263]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found