Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^7: Execute a sub in the background

by chessgui (Scribe)
on Feb 01, 2012 at 16:08 UTC ( [id://951255]=note: print w/replies, xml ) Need Help??


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

I did some research on this because I run chess engines which may get out of hand. I was wondering if I can kill a handler thread when the engine freezes. It is far from being natural and easy. From what I've read I gathered that 'killing' threads from the outside is a thing you should avoid. I personally use the Tk event loop for event driven programming. I only use threads for shepherding processes which are started once and run all the way long paralelly with the MainLoop. Normally there is no need to 'kill' such a thread.

Replies are listed 'Best First'.
Re^8: Execute a sub in the background
by egzoti4en (Initiate) on Feb 01, 2012 at 16:23 UTC

    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

      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://951255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found