#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    45
    Rep Power
    11

    Sending sig INT (control C) to threads


    Hello,
    I've seen the documentation of threads with perl and tryed to do what they said to interrupt a thread after Control-C is pressed.
    http://search.cpan.org/~jdhedden/threads-1.57/threads.pm#THREAD_SIGNALLING
    Unfortunately it makes an error

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    use threads;
    
    sub f1
    {
      $SIG{'INT'} = sub { print "Thread exit\n"; threads->exit(); };
      while(1)
      {
        print "SLEEP\n";
        sleep(2);
      }
    }
    
    my $thr = threads->new('f1');
    $thr->kill('INT')->detach();
    Code:
    $ ./sigint.pl
    Can't locate auto/threads/kill.al in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./new.pl line 19
    A thread exited while 2 threads were running.
    Strange because I created only one thread not two.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    104
    Rep Power
    11
    Ever find a solution for this? I'm stuck on the same thing...

    Oh btw, if you spawn a thread, you're going to have two threads: your new thread, and the main thread of execution.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2005
    Posts
    505
    Rep Power
    119
    Originally Posted by BenZarboni
    Ever find a solution for this? I'm stuck on the same thing...

    Oh btw, if you spawn a thread, you're going to have two threads: your new thread, and the main thread of execution.
    ActiveState 5.8.8 on Win2k

    Code:
    #!/usr/bin/perl -w
    
    use threads;
    use strict;
    use lib 'library';
    use LOG;
    use Smart::Comments;
    
    sub f1 {
      $SIG{INT} = sub {
        ### Thread exit! ...
        threads->exit();
      };
      while(1) {
        ### SLEEPING ...
        sleep(2);
      };
    } # f1: well, Monza is SUNDAY
    
    ### Starting a thread, dude ...
    # AND that "my $thr = threads->new('f1');" is just wrong
    my $thr = threads->new(\&f1);
    ### Wait until the bloody thread has started before you try sending a signal ...
    sleep(1);
    ### Send a signal to the thread ...
    $thr->kill('INT');
    
    ### Collect the bits and pieces! ...
    $_->join foreach threads->list;
    
    __END__
    produces
    Code:
    duh.pl(996) 0 Fri Sep  7 15:49:28 2007 duh.pl(0) << duh.pl >>
    
    duh.pl(996) 0 Fri Sep  7 15:49:28 2007 duh.pl(20) << ### Starting a thread, dude ... >>
    
    duh.pl(996) 0 Fri Sep  7 15:49:28 2007 duh.pl(23) << ### Wait until the bloody thread has started before you try sending a signal ... >>
    
    duh.pl(996) 1 Fri Sep  7 15:49:28 2007 duh.pl(15) << ### SLEEPING ... >>
    
    duh.pl(996) 0 Fri Sep  7 15:49:29 2007 duh.pl(25) << ### Send a signal to the thread ... >>
    
    duh.pl(996) 0 Fri Sep  7 15:49:29 2007 duh.pl(28) << ### Collect the bits and pieces! ... >>
    
    duh.pl(996) 1 Fri Sep  7 15:49:30 2007 duh.pl(11) << ### Thread exit! ... >>
    Yes, the number between the (996) and Fri Sep is the thread id!
    "Plagiarize the code of others, I say!"

    That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad.

    -- Abigail
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    104
    Rep Power
    11
    Hey;

    Thanks for the sample code. Helped me clean some of my stuff up..
    The problem for me however lies elsewhere.


    In your code or mine, whenever I try to CTRL-C and catch that signal + terminate my threads, I get two problems:

    1) The same as the original poster:

    $thr->kill('INT'); produces

    $ ./sigint.pl
    Can't locate auto/threads/kill.al in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./new.pl line 19
    A thread exited while 2 threads were running.
    2) My threads block until they've completed whatever they're doing.. So CTRL-C is blocked until the thread is finished, which prevents me from doing what I want (killing the program immediately)...

    hmm..
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2005
    Posts
    505
    Rep Power
    119
    Post code --- Note that you need to insure that the thread is running and has the SIG handler installed before you attempt the signal ... which is why the sleep 1 in my example.
    "Plagiarize the code of others, I say!"

    That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad.

    -- Abigail
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Рецепты народ


    Здравствуйте! Подскажите пожалуйста, кто сталкивался с: - лечение лекарственными травами - народный целитель Кто пользуется народными средствами? P.s. Перенесите в нужный раздел, если не туда попал Спасибо!

IMN logo majestic logo threadwatch logo seochat tools logo