Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

how to kill a thread in windows

by rjsaulakh (Beadle)
on Jun 23, 2006 at 15:10 UTC ( #557219=perlquestion: print w/replies, xml ) Need Help??
rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

how to kill thread in windows with perl

Replies are listed 'Best First'.
Re: how to kill a thread in windows
by Corion (Pope) on Jun 23, 2006 at 15:18 UTC

    I don't think that's possible with the current Perls (5.8.x and 5.9.3 at this time of writing):

    So maybe this feature is in the most recent version of the threads module, but I doubt it, as it will likely need support in the emulation layer for Perl. One thing I can think of would be to use the Windows API to suspend and/or kill your threads via their threads->tid, but this is very unclean, as Perl also keeps track of your threads and wraps them somewhat. If you really do kill them that way, be prepared for crashes at program exit time.

    Update: Changed ->pid to ->tid as BrowserUk correctly notes below

      One thing I can think of would be to use the Windows API to suspend and/or kill your threads via their threads->pid,

      I guess you meant threads->tid. Even so, that wouldn't work as the thread id it returns is a Perl-internal id, not anything the OS would recognise. However, the cpan version of the threads module does export threads->_handle which could be used in conjunction with Win32::API to Terminate a thread, but like you, I would strongly warn against designing code to use this.

      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?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      The threads module has been greatly improved and enhanced as of late. Signalling between threads is now supported, even on Windows. With this feature, killing a thread is now supported.

      In addition, there is a new Thread::Suspend module that is based on new threads features. It works on Windows, as well.

      Remember: There's always one more bug.
Re: how to kill a thread in windows
by jdhedden (Deacon) on Jun 23, 2006 at 20:58 UTC
    I recently added thead signalling capabilities to the threads module, and improved the functionality of calling exit inside a thread. These features can be used to provide kill/cancel threads as follows:
    use threads; sub thr_func { # Thread 'cancellation' signal handler $SIG{'KILL'} = sub { threads->exit(); }; ... } # Create a thread my $thr = threads->create('thr_func'); ... # Signal the thread to terminate, and then detach # it so that it will get cleaned up automatically $thr->kill('KILL')->detach();
    Be sure to get the latest version of the threads module from CPAN, and read the POD for more information and caveats.

    Remember: There's always one more bug.
Re: how to kill a thread in windows
by zentara (Archbishop) on Jun 23, 2006 at 16:06 UTC
    I don't use windows, but on linux, to end a thread, it needs to reach the end of it's code-block, then be joined. Now if it was detached, there is no need to join it. So "end of code block" or a "return". This can be controlled by using a shared variable, which your main thread sets, is read by the thread, and tells the thread to "return".

    I'm not really a human, but I play one on earth. flash japh
      IMO poster did phrasing very badly, but he meant to kill thread that was not issued by perl,
      so this is even almost off-topic on PM.
Re: how to kill a thread in windows
by OfficeLinebacker (Chaplain) on Jun 23, 2006 at 18:17 UTC
    Isn't there a way to do this using WSH, and doesn't ActiveState Perl have hooks into WSH?


    I like computer programming because it's like Legos for the mind.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://557219]
Approved by Corion
[usemodperl]: this is crazy, the web is broken!
[usemodperl]: just because of banking apps on free wifi, and porn, i guess...
usemodperl thinks every1 should serve HTTP on port 80 incase we don't care about encryption, right? wtf

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-06-24 09:25 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.