Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:07:50+00:00 2026-05-24T13:07:50+00:00

When I use exit or die , it kills the entire program. foreach my

  • 0

When I use exit or die, it kills the entire program.

foreach my $t (threads->list())
{
    $t->exit;
    $count++;
}

Usage: threads->exit(status) at main.pl line 265
Perl exited with active threads:
        9 running and unjoined
        0 finished and unjoined
        0 running and detached

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:07:51+00:00Added an answer on May 24, 2026 at 1:07 pm

    To ignore a thread that is being executed, return back control and throw away whatever it may output, the right method to use is detach, not exit.

    See perldoc perlthrtut – Ignoring a Thread.


    perldoc threads explains why the code exits:

    threads->exit()
    

    If needed, a thread can be exited at any time by calling
    threads->exit(). This will cause the thread to return undef in a
    scalar context, or the empty list in a list context. When called from
    the main thread, this behaves the same as exit(0).


    There may be a way to achieve instant termination (didn’t work for me on Windows):

    use threads 'exit' => 'threads_only';
    

    This globally overrides the default behavior of calling exit()
    inside a thread, and effectively causes such calls to behave the same
    as threads->exit() . In other words, with this setting, calling
    exit() causes only the thread to terminate. Because of its global
    effect, this setting should not be used inside modules or the like.
    The main thread is unaffected by this setting.

    The documentation also suggests another way, using the set_thread_exit_only method (Again, didn’t work for me on Windows):

    $thr->set_thread_exit_only(boolean)
    

    This can be used to change the exit thread only behavior for a thread
    after it has been created. With a true argument, exit() will cause
    only the thread to exit. With a false argument, exit() will
    terminate the application. The main thread is unaffected by this call.


    The example below makes use of a kill signal to terminate the $unwanted thread:

    use strict;
    use warnings;
    use threads;
    
    my $unwanted = threads->create( sub {
                                          local $SIG{KILL} = sub { threads->exit };
                                          sleep 5;
                                          print "Don't print me!\n";
                                        } );
    
    my $valid    = threads->create( sub {
                                          sleep 2;
                                          print "This will print!\n";
                                        } );
    
    $unwanted->kill('KILL')->detach;   # Kills $thr, cleans up
    
    $valid->join;                 # sleep 2, 'This will print!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some code do this: if(something){ echo 'exit from program'; die; }
I want to know how and when can I use the exit() function like
Trying to use: :On Error exit :r D:\opt\db_objects\REPORTS\dbo.sp_ReportCountLORUsers.sql ...and I get: Msg 102, Level
is it possible to use something like an vim-close/exit-Event to execute some last commands
use this website a lot but first time posting. My program creates a number
Consider the following C++ program: #include <cstdlib> // for exit(3) #include <string> #include <iostream>
On Windows (with AcitveState perl 5.8...), when I use system to invoke another program
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.