Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Threads: How to kill a thread manually?

by coldfate (Initiate)
on Apr 26, 2005 at 18:27 UTC ( #451714=perlquestion: print w/ replies, xml ) Need Help??
coldfate has asked for the wisdom of the Perl Monks concerning the following question:

I want to create 2 threads. The first threads continues running until the second thread finishes and then the first one dies.
What I have planned on doing is: (in pseudo-code)
-set default exit code -spawn thread 1 -spawn thread 2 -get exit code from thread 2 -if exit code is from thread 2 - destroy thread 1
I'm not exactly sure how to approach this since I cannot seem to find on any tutorials how to manually kill a thread.
Can someone help me?

Thanks!
coldfate

Edited by Arunbear: Changed title from 'Threads', as per Monastery guidelines

Comment on Threads: How to kill a thread manually?
Download Code
Re: Threads: How to kill a thread manually?
by BrowserUk (Pope) on Apr 26, 2005 at 18:33 UTC
    ... I cannot seem to find on any tutorials how to manually kill a thread.

    There is no api for doing this using threads.

    The best you can do is arrange for your thread(s) to check a shared flag at each iteration and to terminate if that flag becomes set.

    Not so useful if your thread might enter a blocking IO call and so never get a chance to check the flag.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
Re: Threads: How to kill a thread manually?
by Elian (Parson) on Apr 26, 2005 at 18:35 UTC
    You can't kill threads, on purpose -- it's horribly dangerous and pretty much guaranteed to corrupt internal state. Not good.

    What you want is to have thread 1 check the state of thread 2 occasionally and cleanly exit when it's done. (Or, if you just want to completely kill the process, have thread 2 do a hard exit, but that'll not run DESTROY methods and such, which is probably not what you want)

Re: Threads: How to kill a thread manually?
by Booger (Pilgrim) on Apr 26, 2005 at 18:48 UTC

    I realize that this doesn't exactly answer your question, coldfate, but if you can do without threads in whatever it is that you're doing, do so. IMHO, threads are really too complicated for most solutions and defeat the KISS principle.

    Then again, I could be just talking out of my ass.

    (Seeing as how I've never actually had a reason to use them)

      Hmm that might be true. As Elian and BrowserUK pointed out, it seems like what I proposed was a bad idea. Ok, this is my problem.

      My perl program wants to access some data from an external program, B. In order to do so, I need to first run another external program, A, which is required for me to interact with B. A never dies. It continuously runs forever. So it seems like I cannot call:

      system(A);
      system(B);

      otherwise it'll hang on system(A). And it seems like creating 2 separate threads for both A and B and having a successful completion of B kill off A is also not good. Can someone suggest a third alternative to this problem?

      Thanks! coldfate

        If your program doesn't need to interact with program A, then you can detach it, retaining the pid. When your finished with program B, use kill to terminate program A.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?
Re: Threads: How to kill a thread manually?
by Forsaken (Friar) on Apr 26, 2005 at 20:47 UTC
    From the POD documentation for the threads module:

    $thread->join This will wait for the corresponding thread to join. When the thread finishes, join() will return the return values of the entry point function. If the thread has been detached, an error will be thrown. The context (scalar or list) of the thread creation is also the context for join(). This means that if you intend to return an array from a thread, you must use my ($thread) = threads-new(...)>, and that if you intend to return a scalar, you must use my $thread = .... If the program exits without all other threads having been either joined or detached, then a warning will be issued. (A program exits either because one of its threads explicitly calls exit(), or in the case of the main thread, reaches the end of the main program file.)

    Sounds just like what you're looking for. Check out the documentation for threads.
    Remember rule one...
Re: Threads: How to kill a thread manually?
by zentara (Archbishop) on Apr 27, 2005 at 11:36 UTC
    Amplifying on what BrowserUk said, you need to set a shared variable that signals the thread to go to the end of it's code block. Threads must reach the end of their code block to return. Generally you want to be able to have the ability to have the thread 'sleep' , 'go', and 'die'. So set up your worker code in a while loop, if 'go' == 0 , then sleep. If 'go' == 1, do something until it finishes or a 'die' flag == 1. So something like this works (full code in Tk-with-worker-threads ):
    sub worker_thread{ my $dthread = shift; $|++; while(1){ if($shash{$dthread}{'die'} == 1){ goto END }; if ( $shash{$dthread}{'go'} == 1 ){ eval( system( $shash{$dthread}{'data'} ) ); foreach my $num (1..100){ $shash{$dthread}{'progress'} = $num; print "\t" x $dthread,"$dthread->$num\n"; select(undef,undef,undef, .5); if($shash{$dthread}{'go'} == 0){last} if($shash{$dthread}{'die'} == 1){ goto END }; } $shash{$dthread}{'go'} = 0; #turn off self before returning }else { sleep 1 } } END: }

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://451714]
Approved by dragonchild
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-07-14 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls