http://www.perlmonks.org?node_id=557219

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 (Patriarch) 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.