Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 28th, 2006, 04:09 PM
miky miky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 44 miky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 20 m 45 sec
Reputation Power: 10
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.

Reply With Quote
  #2  
Old September 7th, 2007, 01:32 PM
BenZarboni BenZarboni is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 104 BenZarboni User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 47 m 55 sec
Reputation Power: 10
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.

Reply With Quote
  #3  
Old September 7th, 2007, 03:52 PM
Clueless Newbie Clueless Newbie is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2005
Posts: 504 Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 14 m 15 sec
Reputation Power: 118
Quote:
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

Reply With Quote
  #4  
Old September 10th, 2007, 08:21 AM
BenZarboni BenZarboni is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 104 BenZarboni User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 47 m 55 sec
Reputation Power: 10
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

Quote:
$ ./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..

Reply With Quote
  #5  
Old September 10th, 2007, 08:43 AM
Clueless Newbie Clueless Newbie is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2005
Posts: 504 Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level)Clueless Newbie User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 14 m 15 sec
Reputation Power: 118
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.

Reply With Quote
  #6  
Old March 2nd, 2012, 05:35 AM
genaanikev genaanikev is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 1 genaanikev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 56 sec
Reputation Power: 0
Рецепты народ

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Sending sig INT (control C) to threads

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap